繁体   English   中英

在 Spring 启动 java 应用程序中启用 swagger 2 的问题

[英]Issues in Enabling the swagger 2 in Spring Boot java application

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

  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 Object 创建一个 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

I have tried every possible URL of swagger like http://localhost:8080/swagger-ui.html , http://localhost:8080/account/swagger-ui.html , http://localhost:8080/account/v2/api-docs 这些都不起作用。 如果我遗漏了什么或做错了什么,请告诉我。 我真的很感激。

谢谢~~

Swagger 2.9.2 与 spring 启动版本 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

您可以尝试通过将依赖项更新为此

     <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>

而不是@EnableSwagger2 ,将其替换为@EnableSwagger2WebMvc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM