Spring 在不使用最新版本的情况下启动 SwaggerUI

Spring Boot SwaggerUI with not working with latest version

我正在尝试在我的 Spring 引导应用程序中添加 Swagger UI,但我无法访问 swagger-ui.html.

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
</parent>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</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-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-spring-webflux</artifactId>
        <version>3.0.0</version>
    </dependency>

代码:

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.learnings.search.web")).build().pathMapping("/")
                .enableUrlTemplating(false);
    }

}


@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, CassandraAutoConfiguration.class,
        KafkaAutoConfiguration.class })
@EnableSwagger2
public class SearchApp {

    public static void main(String[] args) {
        SpringApplication.run(SearchApp.class, args);
    }
}

我可以访问 swagger 资源:

/swagger-resources
[
  {
    "name": "default",
    "url": "/v2/api-docs",
    "swaggerVersion": "2.0",
    "location": "/v2/api-docs"
  }
]


/swagger-resources/configuration/ui
{
  "deepLinking": true,
  "displayOperationId": false,
  "defaultModelsExpandDepth": 1,
  "defaultModelExpandDepth": 1,
  "defaultModelRendering": "example",
  "displayRequestDuration": false,
  "docExpansion": "none",
  "filter": false,
  "operationsSorter": "alpha",
  "showExtensions": false,
  "showCommonExtensions": false,
  "tagsSorter": "alpha",
  "validatorUrl": "",
  "supportedSubmitMethods": [
    "get",
    "put",
    "post",
    "delete",
    "options",
    "head",
    "patch",
    "trace"
  ],
  "swaggerBaseUiUrl": ""
}

如果最新版本有什么变化请告诉我,因为我在其他项目中一直在使用 swagger-ui。

在 3.0.0 中 swagger-ui.html 已移至 swagger-ui/index.html

如果使用 swagger 3 版本,您还需要添加此依赖项。

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
</dependency>

另外,在Springboot应用的mainclass中添加@EnableOpenApi注解

希望这会奏效。谢谢!