centos

centos swagger API管理

小樊
42
2025-09-01 23:36:45
栏目: 智能运维

在CentOS上进行Swagger API管理,可参考以下步骤:

安装环境

若使用Java语言开发,需安装Java和Maven,命令为:

sudo yum install -y java-1.8.0-openjdk-devel maven

搭建Swagger

以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>

配置Swagger

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

0
看了该问题的人还看了