Unable to get Swagger UI working with Spring boot

北慕城南 提交于 2019-12-31 08:58:07

问题


I am trying to get Swagger UI working with Spring Boot 1.2.1. I followed the instructions at https://github.com/martypitt/swagger-springmvc and I added @EnableSwagger on my spring config.

I currently get back JSON when I go to http://localhost:8080/api-docs but no nice HTML.

I am using Maven and added the dependency on swagger-ui:

<dependency>
    <groupId>org.ajar</groupId>
    <artifactId>swagger-spring-mvc-ui</artifactId>
    <version>0.4</version>
</dependency>

This is my complete list of dependencies:

<dependencies>
        <dependency>
            <groupId>com.mangofactory</groupId>
            <artifactId>swagger-springmvc</artifactId>
            <version>0.9.4</version>
        </dependency>
        <dependency>
            <groupId>org.ajar</groupId>
            <artifactId>swagger-spring-mvc-ui</artifactId>
            <version>0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

I also tried http://localhost:8080/docs/index.html as URL, but that just gives the "Whitelabel Error Page"

Update:

I created a test project on Github to show the problem: https://github.com/wimdeblauwe/springboot-swagger-test


回答1:


Your problem lies in your SwaggerConfiguration file. You need to take out @EnableWebMvc, because this causes the default Spring Boot view resolver to be overwritten by the default 'SpringWebMvc' one which serves static content differently.

By default, Spring Boot will serve static content from any of the following directories:

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

including webjars.

I had the same problem and I found this in the documentation: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Bean of type WebMvcConfigurerAdapter, but without @EnableWebMvc.

I hope this helps.




回答2:


I have swagger-ui v0.4 (with spring v4.14 & swagger-springmvc v0.9.4) working fine, although I had some similar problems at first. It seems like this class does the trick.

@Configuration
@EnableSwagger
public class SwaggerConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private SpringSwaggerConfig springSwaggerConfig;

    @Bean
    public SwaggerSpringMvcPlugin customImplementation() {
        return new SwaggerSpringMvcPlugin(springSwaggerConfig).apiInfo(
                apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(/* strings */);
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

I believe the relevant thing is the overridden configureDefaultServletHandling. And on my main WebApplicationInitializer, I have:

@Import(SwaggerConfig.class)

Finally, I fixed the issue with the UI's location box showing "http://localhost:8080${pageContext.request.contextPath}/api-docs" by including this in my dependencies:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <!--<version>8.0.15</version>-->
    <scope>provided</scope>
</dependency>

That provides something related to JSP processing. It is included in the dependencies of spring-boot, but it isn't normally provided.

Hope that helps.




回答3:


I have the same issue but this link works for me: http://localhost:8080/sdoc.jsp

It pre-populates the swagger ui resource url box with : http://localhost:8080${pageContext.request.contextPath}/api-docs

and when I hand edit it removing ${pageContext.request.contextPath} and hit Explore it shows me my api doc and I can even try my endpoints successfully. So definitely an issue but probably not picking up ${pageContext.request/contextPath}.

Looking at the source the javascript has: url: window.location.origin + "${pageContext.request.contextPath}/api-docs"

on a static swagger ui html I have this piece is coded as:

discoveryUrl:"./resource-list.json"

I hope this is a bit helpful




回答4:


I have done separate config for Swagger and my problem was that without @EnableAutoConfiguration it was not working properly.

@Configuration
@EnableSwagger2
@EnableAutoConfiguration
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}



回答5:


I would suggest you to use @EnableSwagger2 tag and follow the steps and code from here: https://github.com/sanketsw/SpringBoot_REST_API

Also i am using following dependency which works perfectly fine:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.2.2</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.2.2</version>
    <scope>compile</scope>
</dependency>


来源:https://stackoverflow.com/questions/27861872/unable-to-get-swagger-ui-working-with-spring-boot

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