Spring Boot 3 springdoc-openapi-ui 不起作用

Nad*_*dar 86 java spring swagger-ui spring-boot

我正在尝试将 swagger-ui (OpenAPI 3.0) 添加到 Spring Boot v3 应用程序中。

我已经添加了 openapi-ui maven 依赖项,它应该按照文档工作。

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.11</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

但显然,它仍然不起作用,并且 localhost:8080/swagger-ui.html 返回 404 错误。

我缺少什么?

在此输入图像描述

Sus*_*afa 129

根据文档:

对于spring-boot 3支持,请确保使用springdoc-openapi v2

springdoc-openapi 与 spring-boot 的兼容性矩阵是怎样的?

对于 spring-boot 和 swagger-ui 之间的集成,请将库添加到项目依赖项列表中(无需额外配置)

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这将自动将 swagger-ui 部署到 spring-boot 应用程序:

文档将以 HTML 格式提供,使用官方 swagger-ui jar

然后,Swagger UI 页面将在以下位置提供, http://server:port/context-path/swagger-ui.html并且 OpenAPI 描述将在以下 json 格式的 url 中提供: http://server:port/context-path/v3/api-docs

server: The server name or IP

port: The server port

context-path: The context path of the application

Documentation can be available in yaml format as well, on the following path : /v3/api-docs.yaml
Run Code Online (Sandbox Code Playgroud)

请注意,模块已重命名:

https://springdoc.org/#migration-from-springdoc-v1

在此输入图像描述

  • 这种奇怪的。根据文档 https://springdoc.org/ 的归纳部分,它指出了这一点。库支持 OpenAPI 3、Spring-boot(v1、v2 和 v3),这显然不是事实。在 Spring Boot 3 环境中,Spring 工厂发生了一些变化,并且 springdoc bean 根本没有实例化。 (4认同)
  • 确保将 springdoc-openapi-ui 依赖项替换为 springdoc-openapi-starter-webmvc-ui 依赖项。如果您同时使用它们,应用程序将无法启动 (4认同)
  • 即使使用 Spring Boot 3 和正确的依赖项,仍然无法为我工作。 (2认同)
  • 不要像我一样笨蛋 - 使用访问:_http://server:port/swagger-ui/index.html_。升级后,我不断输入 &lt;context-path&gt; : _http://server:port/&lt;contextpath&gt;/swagger-ui/index.html_ 。浪费了大约半天的时间。 (2认同)

小智 21

我完全同意@JCompetence 的观点。请注意, springdoc-openapi-ui现在 从 spring boot 3更改为springdoc-openapi-starter-webmvc-ui 。

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

请尝试这个。如果您想了解更多信息,请查看官方链接: https ://springdoc.org/v2/#features

  • 该链接不再有效。尝试不使用 /v2 (4认同)

小智 11

对我来说它有帮助,只是改变了依赖关系

   implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.11'
Run Code Online (Sandbox Code Playgroud)

    implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.0.0'
Run Code Online (Sandbox Code Playgroud)

或者

   implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.11'
Run Code Online (Sandbox Code Playgroud)


Ari*_*dar 9

您需要使用 springdoc-openapi

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

对于 kotlin 你必须再添加一个依赖项

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-kotlin</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

然后在你的主类上添加`@OpenAPIDefinition

@SpringBootApplication
@OpenAPIDefinition
class MyApplication {
}
Run Code Online (Sandbox Code Playgroud)


小智 6

springdoc-openapi-starter-webmvc-uispring-boot-starter-webflux 除非您包含 ,否则无法使用spring-boot-starter-web。如果你包括了 Spring Security 那么它就死了。