在Ubuntu系統下生成Swagger API文檔,通常需要根據你所使用的編程語言和框架來選擇相應的工具和方法。以下是幾種常見的方法:
sudo apt update
sudo apt install nodejs npm
下載并安裝Swagger Editor:
http-server。訪問Swagger Editor:
打開瀏覽器,訪問 http://localhost:8080(具體端口可能根據你的設置有所不同)。
導入或創建Swagger文檔: 你可以導入現有的Swagger JSON或YAML文件,或者創建一個新的文檔。
編輯和查看文檔: 在Swagger Editor中直接編輯你的API文檔,然后保存并查看。
go install github.com/swaggo/swag/cmd/swag@latest
docs.go 文件,其中包含了Swagger文檔的代碼。swag init
// @Summary 獲取用戶信息
// @Description 獲取用戶信息
// @Tags Users
// @Accept json
// @Produce json
// @Param id path int true "用戶ID"
// @Success 200 {object} User
// @Router /users/{id} [get]
swag init 命令,swag工具會根據你的注釋生成相應的Swagger文檔。pom.xml 文件中添加Springfox的依賴。<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
@EnableSwagger2 注解啟用Swagger,并配置文檔的基本信息和掃描包的路徑。@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("cn.weijishu.server.api.rest"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Weijishu Swagger API Document")
.description("Weijishu Swagger Description...")
.contact(new Contact("Weijishu", "https://www.weijisu.cn/", "mail@weijishu.cn"))
.version("0.1")
.build();
}
}
@Api(tags ["SwaggerDemo"])
@RestController
@RequestMapping("/weijishu")
public class WeijishuSwagger2Controller {
@ApiOperation(value = "方法說明", notes = "通過A")
@PostMapping("/add")
public ResponseEntity<String> add(@RequestBody String requestBody) {
// 你的業務邏輯
return ResponseEntity.ok("Success");
}
}
http://localhost:8080/swagger-ui.html 訪問生成的Swagger文檔。希望這些信息能幫助你在Ubuntu系統下成功生成Swagger API文檔。