Reputation: 103
I'm trying to integrate swagger 2 in my spring-mvc webapp (no spring boot).
It works when I go to /myApp/v2/api-odcs, I see the version, description and all my restControllers.
But when I try to access /myApp/swagger-ui.html All what I get is a blank page without even the green nav bar.
When I monitor the network I see that it get all resources (swagger-ui.html, css and js) succesfully (status 200) and it show the title in the tab : "MyApp: Swagger UI".
here's my configuration:
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build()
.pathMapping("/")
.apiInfo(apiInfo())
.useDefaultResponseMessages(false);
}
@Bean
public ApiInfo apiInfo() {
final ApiInfoBuilder builder = new ApiInfoBuilder();
builder
.title("MyApp - API")
.description("desc")
.version("1.0");
return builder.build();
}
}
and here are my dependencies :
compile group: 'org.webjars', name: 'swagger-ui', version: '3.5.0'
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'
Upvotes: 2
Views: 5037
Reputation: 1846
Here is my configuration, if that helps. I also specified the host and the path provider.
@Configuration
@EnableSwagger2
@Profile({"!production"})
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {
@Autowired
private ServletContext servletContext;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.host("localhost")
.directModelSubstitute(LocalDate.class, Date.class)
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/myApp";
}
})
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Note: If this is a SpringMVC application you may also need to remove @EnableWebMvc
https://github.com/springfox/springfox/issues/776
Upvotes: 1