Skip to content

==推荐使用== springdoc-openapi ==的理由如下:==

  • springdoc-openapi ==是== spring ==官方出品,与== springboot ==兼容更好(==springfox ==兼容有坑)==
  • springdoc-openapi ==社区更活跃,==springfox ==已经== 2 ==年没更新了==
  • springdoc-openapi ==的注解更接近== OpenAPI 3 ==规范==

使用,pom.xml中添加依赖:

  • <dependency>
  • <groupId>org.springdoc</groupId>
  • <artifactId>springdoc-openapi-ui</artifactId>
  • <version>1.6.11</version>
  • </dependency>

实测发现可配可不配:然后在 Config 中配置基本的描述信息,如下:

@Configuration public class OpenApiConfig { @Bean public OpenAPI springOpenAPI() { return new OpenAPI() .info(new Info() .title("SpringDoc API Test") .description("SpringDoc Simple Application Test") .version("0.0.1")); } } 接下来在 Controller 中使用注解标记文本,如下:

@RestController(value = "/clients") @Tag(name = "/clients") public class ClientsRestController { @Operation(summary = "This method is used to get the clients.") @GetMapping public List<String> getClients() { return Arrays.asList("First Client", "Second Client"); } } 最后 Application.java 启动应用后,输入默认地址:http://localhost:8081/swagger-ui/index.html 即可看到文档;

这将自动将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

  • yaml格式文档地址:

http://server:port/context-path/v3/api-docs.yaml 其中:

  • server: The server name or IP
  • port: The server port
  • context-path: The context path of the application

对于HTML格式swagger文档的自定义路径,可以在application.yml中配置:

  • swagger-ui custom path ​

  • springdoc.swagger-ui.path=/swagger-ui.html

版本配置Swagger UI

  • swagger官方属性的支持可在springdoc-openapi上获得。看官方文档:Swagger Documentation
  • 您可以在文档中使用与Spring Boot属性相同的swagger属性。
  • 所有这些属性都应该用下面的前缀来声明: springdoc.swagger-ui

本站总访问 — 次