在CentOS上進行Swagger API管理,可參考以下步驟:
若使用Java語言開發,需安裝Java和Maven,命令為:
sudo yum install -y java-1.8.0-openjdk-devel maven
以Spring Boot項目為例,添加Swagger依賴到pom.xml:
<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>
創建SwaggerConfig類:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
在代碼中使用Swagger注解,如@Api、@ApiOperation等描述API,啟動應用后,訪問http://localhost:8080/swagger-ui.html可查看文檔。