【问题标题】:Swagger with SpringBoot Version 1.2.2 is not working使用 SpringBoot 版本 1.2.2 的 Swagger 无法正常工作
【发布时间】:2020-07-03 12:39:28
【问题描述】:

我正在尝试将 swagger 集成到我的 Spring Boot 应用程序中。

春季版: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()
 

Swagger-UI URL 仍然返回 404。

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

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

P.S:在谷歌上搜索了这么多页面后,我怀疑它可能是版本兼容性。如果是这样,我无法升级我的 spring-boot 版本。在那种情况下,我该如何集成 Swagger?有什么建议吗?

【问题讨论】:

    标签: java spring-boot swagger swagger-2.0 springfox


    【解决方案1】:

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

    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>
    

    【讨论】:

    • 好的,我现在可以成功集成它,只需要很少的额外配置。我会更新答案。感谢您的回复。
    • 很高兴知道这一点
    【解决方案2】:

    我添加了以下附加配置,

    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 配置类,允许所有 URL

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

    http://localhost:8080/&lt;projectName&gt;/swagger-ui.html#/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多