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

前端 未结 5 904
广开言路
广开言路 2020-12-29 23:14

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

相关标签:
5条回答
  • 2020-12-29 23:42

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

    0 讨论(0)
  • 2020-12-29 23:46

    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/");
    
        }
    
    0 讨论(0)
  • 2020-12-29 23:49

    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.

    0 讨论(0)
  • 2020-12-29 23:49

    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.

    0 讨论(0)
  • 2020-12-29 23:56

    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.

    0 讨论(0)
提交回复
热议问题