Swagger2(Api文档)

目录

一、配置Swagger2

        1、Swagger是什么?

        2、Swagger2文档代码

        3、常用注解

        4、配置pom.xml文件

        5、swagger注解会生成文档方便参考


一、配置Swagger2

        1、Swagger是什么?

        编写和维护接口文档,根据Swagger2可以快速帮助我们编写最新的API接口文档,再也不用担心开会前仍忙于整理各种资料了,间接提升了团队开发的沟通效率。

        2、Swagger2文档代码

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket adminApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")//Api分组
                .apiInfo(adminApiInfo())
                .select() //选择过滤
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))//选择过滤路径
                .build();
    }

    //描述文档adminApi标签封装的方法
    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                .title("HZFinance-后台管理系统Api文档")
                .description("该文档描述了HZFinance后台管理系统各个模块接口调用的方式")
                .version("1.0")
                .contact(new Contact("HaoZun","https://nmhxs.com","nmhxs999@.163.com"))
                .build();
    }

    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/api/.*")))
                .build();
    }

    //描述webApi标签封装的方法
    private ApiInfo webApiInfo(){
        return new  ApiInfoBuilder()
                .title("HZFinance-网站Api文档")
                .description("该文档描述了HZFinance网站各个模块接口调用的方式")
                .version("1.0")
                .contact(new Contact("HaoZun","https://nmhxstest.com","nmhxs999@.163.com"))
                .build();
    }
}

分类管理Api

 

        3、常用注解

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数

        4、配置pom.xml文件

<!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <!--swagger ui-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
    </dependencies>

        5、swagger注解会生成文档方便参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值