SpringBoot-7. 使用Swagger构建文档

swagger可以降低我们前后端开发文档同步问题,其能够根据我们的代码注释自动生成API文档,而我们只需简单的运用其注释即可,可视化的呈现程序所有的接口列表,非常方便我们前后端进行接口调试。

The Best APIs are Built with Swagger Tools

Swagger工具可以帮助您完成生成和维护API文档的工作,确保您的文档在API发展过程中保持最新状态。

1. 添加Swagger依赖

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

2. 新建SwaggerConfig类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfo(
                "Spring Boot Swagger Test",
                "我的博客网站:https://zyz-1998.github.io/,欢迎大家访问。",
                "API V1.0",
                "Terms of service",
                new Contact("zyz", "https://zyz-1998.github.io/", "1744020412@qq.com"),
                "Apache", "http://www.apache.org/", Collections.emptyList());
    }
}

3. 添加文档内容

用于Controller(控制类)

注解作用
@Api表示一个开放的API
@ApiOperation给API增加说明,表示针对该API的CRUD操作
@ApiResponses描述API操作可能出现的异常情况
@ApiImplicitParams@ApiImplicitParam给API参数增加说明。

用于model(实体类)

注解作用
@ApiModel注解实体
@ApiModelProperty定义实体属性

4. 访问验证

http://localhost:8080/swagger-ui.html#/

效果如下:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值