【问题标题】:Issues in Enabling the swagger 2 in Spring Boot java application在 Spring Boot java 应用程序中启用 swagger 2 的问题
【发布时间】:2020-11-07 15:48:36
【问题描述】:

我希望你们一切都好。我试图在我的弹簧靴中启用招摇,但不知何故我无法让它工作。我采取了我应该采取的所有步骤,现在坚持一天真的很沮丧。以下是详细信息。

  1. 添加的依赖项
<dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-swagger-ui</artifactId>
           <version>2.9.2</version>
       </dependency>
       <dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-swagger2</artifactId>
           <version>2.9.2</version>
       </dependency>
  1. 启用 swagger 注释
@EnableSwagger2
  1. 使用要扫描的 Docket 对象创建 bean
@Bean
    public Docket swaggerApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("ml.XXXXXXXX"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder().version("v1")
                        .title("Account Service API")
                        .description("Documentation Account Service API v1").build());
    }

我的 servlet 上下文路径是

server:
  servlet:
    context-path: /account

我已经尝试了所有可能的招摇网址,例如http://localhost:8080/swagger-ui.html、http://localhost:8080/account/swagger-ui.html、http://localhost:8080/account/v2/api-docs。这些都不起作用。如果我遗漏了什么或做错了什么,请告诉我。我真的很感激。

谢谢~~

【问题讨论】:

  • 什么版本的spring boot?如果您将日志记录设置为调试,您可能会看到配置没有被拾取。米
  • 它是 2.3.1.RELEASE,实际上在日志中它确实显示它是这样映射的:映射 URL 路径 [/v2/api-docs] 到方法 [springfox.documentation.swagger2.web。 Swagger2Controller#getDocumentation(String, HttpServletRequest)]
  • 看起来它正在招摇,但不是 UI。也许下面的博客可能会帮助baeldung.com/swagger-2-documentation-for-spring-rest-api
  • 是的,在这种情况下 /v2/api-docs 的端点应该仍然可以工作,但它不是:(
  • 我从未使用过 Swagger 2,但它可能需要在 POM 中配置一个插件才能正常工作。

标签: java spring spring-boot swagger


【解决方案1】:

你可以通过更新依赖来尝试吗

     <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-spring-webmvc</artifactId>
        <version>3.0.0-SNAPSHOT</version>
    </dependency>

用@EnableSwagger2WebMvc代替@EnableSwagger2

【讨论】:

    【解决方案2】:

    Swagger 2.9.2 无法正常使用 Spring Boot 2.0 及以上版本,所以我切换到 Open API。它只需要 2 个步骤

    1.添加依赖

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.4.3</version>
    </dependency>   
    
    1. 更新/设置 URL 属性

       springdoc:
         version: '@springdoc.version@'
         api-docs:
           enabled: true
           path: /v3/api-docs
         swagger-ui:
           path: /swagger-ui.html
      

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 2015-11-03
      • 1970-01-01
      • 2021-04-25
      • 2021-01-07
      • 1970-01-01
      • 2019-07-25
      • 2018-11-19
      • 2020-10-19
      相关资源
      最近更新 更多