在Debian系统下生成Swagger的API文档,通常需要根据你使用的编程语言和框架来选择相应的工具和步骤。以下是针对不同框架的简要指南:
sudo apt update
sudo apt install openjdk-11-jdk maven
创建Spring Boot项目: 使用Spring Initializr创建一个新的Spring Boot项目,并添加Spring Web依赖。
添加Swagger依赖:
在pom.xml
文件中添加Swagger依赖。
<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>
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build();
}
}
mvn spring-boot:run
http://localhost:8080/swagger-ui.html
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
@Api(tags = "示例控制器")
public class ExampleController {
@GetMapping("/hello")
@ApiOperation(value = "返回Hello World", notes = "返回Hello World的示例")
public String sayHello() {
return "Hello, World!";
}
}
pip install Flask flasgger
from flasgger import Swagger
from flask import Flask
app = Flask(__name__)
swagger = Swagger(app)
from flasgger import swag_from
@app.route('/hello_world')
@swag_from('swagger.yaml')
def hello_world():
"""This is a simple hello world endpoint.
---
responses:
200:
description: A successful response
content:
application/json:
schema:
type: object
properties:
message:
type: string
"""
return {'message': 'Hello, World!'}
http://localhost:5000/apidocs/
请注意,上述步骤可能需要根据你的具体项目环境和使用的框架进行调整。此外,Swagger的版本和工具的具体配置选项可能会有所不同,因此建议查阅相关文档以获取最新和最准确的信息。