在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
可查看文档。