Unable to bring up swagger-ui from spring-boot application

孤者浪人 提交于 2019-11-30 08:24:12

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/");

    }
Dilip Krishnan

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 it's 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.

In my case I removed spring.resources.static-locations in properties file and it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!