在 Debian 上“用 Swagger 生成文档”通常有两种常见场景,我分别说明,你按需选择即可。
依赖(Maven)
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example"))
.paths(PathSelectors.any())
.build();
}
}
访问
http://localhost:8080/swagger-ui.html
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
访问
http://localhost:8080/swagger-ui.html
或
http://localhost:8080/swagger-ui/index.html
sudo apt update
sudo apt install npm
sudo npm install -g @redocly/cli
# 或
sudo npm install -g swagger-cli
openapi.yaml
openapi: 3.0.0
info:
title: User API
version: 1.0.0
paths:
/users:
get:
summary: 获取用户列表
responses:
'200':
description: OK
redocly build-docs openapi.yaml -o api.html
或使用 Swagger UI:
npx swagger-cli validate openapi.yaml
git clone https://github.com/swagger-api/swagger-ui.git
cp swagger-ui/dist/* /var/www/html/
把 openapi.json 放到目录中并修改 index.html:
url: "openapi.json"
# 校验
swagger-cli validate openapi.yaml
# 生成文档
redocly build-docs openapi.yaml -o doc.html
如果你能告诉我:
我可以给你更精确的方案。