Skip to content

Commit 27c7e69

Browse files
committed
updated templates
1 parent 5a5bb6e commit 27c7e69

File tree

9 files changed

+110
-4
lines changed

9 files changed

+110
-4
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/SpringMVCServerCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public SpringMVCServerCodegen() {
6868
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "WebApplication.java"));
6969
supportingFiles.add(new SupportingFile("webMvcConfiguration.mustache",
7070
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "WebMvcConfiguration.java"));
71+
supportingFiles.add(new SupportingFile("swaggerUiConfiguration.mustache",
72+
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerUiConfiguration.java"));
7173
supportingFiles.add(new SupportingFile("swagger.properties",
7274
("src.main.resources").replace(".", java.io.File.separator), "swagger.properties"));
7375

modules/swagger-codegen/src/main/resources/JavaSpringMVC/pom.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@
183183
<artifactId>springfox-swagger2</artifactId>
184184
<version>${springfox-version}</version>
185185
</dependency>
186+
<dependency>
187+
<groupId>io.springfox</groupId>
188+
<artifactId>springfox-swagger-ui</artifactId>
189+
<version>${springfox-version}</version>
190+
</dependency>
186191

187192
<dependency>
188193
<groupId>org.scalatest</groupId>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
springfox.documentation.swagger.v2.path=/swagger.json
1+
springfox.documentation.swagger.v2.path=/api-docs

modules/swagger-codegen/src/main/resources/JavaSpringMVC/swaggerConfig.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.ComponentScan;
55
import org.springframework.context.annotation.Configuration;
66
import org.springframework.context.annotation.PropertySource;
7+
import org.springframework.context.annotation.Import;
78
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
89
import springfox.documentation.service.ApiInfo;
910
import springfox.documentation.spi.DocumentationType;
@@ -16,8 +17,8 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
1617
@EnableWebMvc
1718
@EnableSwagger2 //Loads the spring beans required by the framework
1819
@PropertySource("classpath:swagger.properties")
20+
@Import(SwaggerUiConfiguration.class)
1921
public class SwaggerConfig {
20-
2122
@Bean
2223
ApiInfo apiInfo() {
2324
ApiInfo apiInfo = new ApiInfo(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package {{configPackage}};
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
5+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
8+
9+
@Configuration
10+
@EnableWebMvc
11+
public class SwaggerUiConfiguration extends WebMvcConfigurerAdapter {
12+
private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
13+
14+
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
15+
"classpath:/META-INF/resources/", "classpath:/resources/",
16+
"classpath:/static/", "classpath:/public/" };
17+
18+
private static final String[] RESOURCE_LOCATIONS;
19+
static {
20+
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
21+
+ SERVLET_RESOURCE_LOCATIONS.length];
22+
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
23+
SERVLET_RESOURCE_LOCATIONS.length);
24+
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
25+
SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
26+
}
27+
28+
private static final String[] STATIC_INDEX_HTML_RESOURCES;
29+
static {
30+
STATIC_INDEX_HTML_RESOURCES = new String[RESOURCE_LOCATIONS.length];
31+
for (int i = 0; i < STATIC_INDEX_HTML_RESOURCES.length; i++) {
32+
STATIC_INDEX_HTML_RESOURCES[i] = RESOURCE_LOCATIONS[i] + "index.html";
33+
}
34+
}
35+
36+
@Override
37+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
38+
if (!registry.hasMappingForPattern("/webjars/**")) {
39+
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
40+
}
41+
if (!registry.hasMappingForPattern("/**")) {
42+
registry.addResourceHandler("/**").addResourceLocations(RESOURCE_LOCATIONS);
43+
}
44+
}
45+
46+
}

samples/server/petstore/spring-mvc/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@
183183
<artifactId>springfox-swagger2</artifactId>
184184
<version>${springfox-version}</version>
185185
</dependency>
186+
<dependency>
187+
<groupId>io.springfox</groupId>
188+
<artifactId>springfox-swagger-ui</artifactId>
189+
<version>${springfox-version}</version>
190+
</dependency>
186191

187192
<dependency>
188193
<groupId>org.scalatest</groupId>

samples/server/petstore/spring-mvc/src/main/java/io/swagger/configuration/SwaggerConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.context.annotation.ComponentScan;
55
import org.springframework.context.annotation.Configuration;
66
import org.springframework.context.annotation.PropertySource;
7+
import org.springframework.context.annotation.Import;
78
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
89
import springfox.documentation.service.ApiInfo;
910
import springfox.documentation.spi.DocumentationType;
@@ -16,8 +17,8 @@
1617
@EnableWebMvc
1718
@EnableSwagger2 //Loads the spring beans required by the framework
1819
@PropertySource("classpath:swagger.properties")
20+
@Import(SwaggerUiConfiguration.class)
1921
public class SwaggerConfig {
20-
2122
@Bean
2223
ApiInfo apiInfo() {
2324
ApiInfo apiInfo = new ApiInfo(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.swagger.configuration;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
5+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
8+
9+
@Configuration
10+
@EnableWebMvc
11+
public class SwaggerUiConfiguration extends WebMvcConfigurerAdapter {
12+
private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
13+
14+
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
15+
"classpath:/META-INF/resources/", "classpath:/resources/",
16+
"classpath:/static/", "classpath:/public/" };
17+
18+
private static final String[] RESOURCE_LOCATIONS;
19+
static {
20+
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
21+
+ SERVLET_RESOURCE_LOCATIONS.length];
22+
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
23+
SERVLET_RESOURCE_LOCATIONS.length);
24+
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
25+
SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
26+
}
27+
28+
private static final String[] STATIC_INDEX_HTML_RESOURCES;
29+
static {
30+
STATIC_INDEX_HTML_RESOURCES = new String[RESOURCE_LOCATIONS.length];
31+
for (int i = 0; i < STATIC_INDEX_HTML_RESOURCES.length; i++) {
32+
STATIC_INDEX_HTML_RESOURCES[i] = RESOURCE_LOCATIONS[i] + "index.html";
33+
}
34+
}
35+
36+
@Override
37+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
38+
if (!registry.hasMappingForPattern("/webjars/**")) {
39+
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
40+
}
41+
if (!registry.hasMappingForPattern("/**")) {
42+
registry.addResourceHandler("/**").addResourceLocations(RESOURCE_LOCATIONS);
43+
}
44+
}
45+
46+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
springfox.documentation.swagger.v2.path=/swagger.json
1+
springfox.documentation.swagger.v2.path=/api-docs

0 commit comments

Comments
 (0)