【问题标题】:Swagger UI page is found for Spring Boot 2为 Spring Boot 2 找到 Swagger UI 页面
【发布时间】:2021-01-03 09:22:21
【问题描述】:

使用 Spring Boot 2.3.1.

这是来自pom的sn-p:

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

swagger 版本是last for now: 3.0.0

Swagger 配置:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Bean
    public Docket swaggerApiDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
                .build()
                .apiInfo(apiDetails());
    }

    private ApiInfo apiDetails() {
        return new ApiInfo("Carpark Controller API",
                "Carpark Service for managing car parks",
                "0.0.1",
                "",
                new springfox.documentation.service.Contact("Jan",
                        "www.demo.com",
                        ""),
                "API License",
                "",
                Collections.emptyList());
    }
}

未添加安全配置。没有任何server-path 或一些额外的配置。

当应用程序启动时,可以使用 swagger JSON 文档:

http://localhost:8080/v2/api-docs

但是,如果要导航到 swagger UI:

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

结果将是:

出现意外错误(类型=未找到,状态=404)。

尝试将 swagger 版本降级为2.9.2 结果是一样的。

如何解决这个问题?

【问题讨论】:

标签: spring spring-boot swagger http-status-code-404 spring-boot-2


【解决方案1】:

找到 Spring Boot 2 的解决方案:

  1. 从 pom 文件中读取所有其他 swagger 依赖项。只添加下一个:

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

Swagger 配置可能与之前使用 @EnableSwagger2 时相同。

  1. 启动应用程序。

  2. Swagger UI 页面现在不同了:

    http://localhost:8080/swagger-ui/index.html

终于成功了!


在网上寻找这个问题的解决方案找到了following comment

查看 Spring Boot 版本的最新示例:BootWebmvcApplication

还有build.gradle configuration

这是other projects sources的链接。

【讨论】:

  • 它对我不起作用......仍然只有“v2/api-docs”端点没有招摇 UI。
  • ... 嗯,Swagger 2 和 Spring boot 2 好像有兼容性问题:github.com/springfox/springfox/issues/3052
  • 版本兼容 spring boot at question 和 springfox as answer
【解决方案2】:

在 POM.XML 文件中,您错过了一个具有 swagger starter 的依赖项

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

【讨论】:

  • 此依赖的版本是强制性的,没有它就无法工作。
猜你喜欢
  • 1970-01-01
  • 2023-01-04
  • 2021-12-31
  • 2018-01-27
  • 1970-01-01
  • 2020-03-08
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多