Swagger 与 SpringBoot 版本 1.2.2 不工作

Swagger with SpringBoot Version 1.2.2 is not working

我正在尝试将 swagger 集成到我的 spring 启动应用程序中。

Spring版本:1.2.2.RELEASE Java版本:8

Swagger Maven 依赖

<swagger.version>2.0.1</swagger.version>
<swagger-annotations.version>1.5.21</swagger-annotations.version>
<swagger-models.version>1.5.21</swagger-models.version>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>${swagger-annotations.version}</version>
</dependency>
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-models</artifactId>
    <version>${swagger-models.version}</version>
</dependency>

配置文件是

@Configuration
@EnableSwagger2
@EnableAutoConfiguration
public class Swagger2UiConfiguration {


    public Docket postsApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("public-api").select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket apiDoc(ServletContext servletContext) {
        return new Docket(DocumentationType.SWAGGER_2)
                .host("http://localhost:8080")
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return "/services";
                    }
                });
    }

    private Predicate<String> postPaths() {
        return or(regex("/myproject/services/*"));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("EMP").description("EMP Swagger Test").version("1.0").build();
    }

}

当运行应用程序我可以看到以下日志,

2020-07-03 20:50:29,218 [ INFO] [] [localhost-startStop-1] (AbstractHandlerMethodMapping.java:220) - Mapped "{[/v2/api-docs],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json || application/hal+json],custom=[]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)
2020-07-03 20:50:29,225 [ INFO] [] [localhost-startStop-1] (AbstractHandlerMethodMapping.java:220) - Mapped "{[/swagger-resources/configuration/security],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
2020-07-03 20:50:29,227 [ INFO] [] [localhost-startStop-1] (AbstractHandlerMethodMapping.java:220) - Mapped "{[/swagger-resources/configuration/ui],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
2020-07-03 20:50:29,228 [ INFO] [] [localhost-startStop-1] (AbstractHandlerMethodMapping.java:220) - Mapped "{[/swagger-resources],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
 

还是招摇-UIURLreturns404.

http://localhost:8080/swagger-ui.html
http://localhost:8080/myproject/services/swagger-ui.html

我错过了什么吗?任何帮助都非常有用。

P.S: google了这么多页,怀疑是版本兼容问题。如果是这样,我无法升级我的 spring-boot 版本。在那种情况下,我该如何集成 Swagger?有什么建议吗?

我认为您的依赖性有问题。您只需要两个依赖项。
我只使用这两个依赖项。

  1. springfox-swagger2
  2. springfox-swagger-ui
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.version}</version>
</dependency>

我添加了以下附加配置,

  1. Maven 依赖,
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.6.1</version>
</dependency>
  1. Swagger 配置 class,允许所有 URLs

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    
  2. 访问了以下URL,

http://localhost:8080/<projectName>/swagger-ui.html#/