I have a spring boot application that i am running using embedded tomcat server. I am partially successful in getting springfox-swagger integrated with the app. If i do a
In my case I removed spring.resources.static-locations
in properties file and it works.
If you want to keep @EnableWebMvc annotation any way, you have to add the following
@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/");
}
springfox-swagger-ui is a web jar and requires that you set up resource handlers to inform the dispatch servlet how and which resource to serve up when you ask for ../swagger-ui.html
. Usually in a spring-boot application auto-configuration takes care of setting it up for you. The reason its not loading in your case is because you've signaled to spring-boot that the application is going to be manually configured via the WebMvcConfigurerAdapter
/@EnableWebMvc
combination.
You should be able to place the @SpringBootApplication
annotation on your main spring configuration and get rid of the WebConfig
class all-together.
Since your WebConfig
isn't adding any value other than making sure the JSON is indented, I'd suggest removing it all-together and replacing it with a Jackson2ObjectMapperBuilder bean instead.
For examples on how to do the same thing in spring-mvc/spring-boot etc. take a look at the springfox-demos project. In particular take a look at SpringConfig to see how to manually configure the resource handlers.
I have encountered this problem before and the problem was in the below line in application.properties
:
spring.resources.add-mappings=false
Remove it or change its value to true.
It worked for me, when i dint override the static path pattern.
In spring boot application just avoid "spring.mvc.static-path-pattern" from properties file and swagger-ui will works fine.