SwaggerUI 自动生成文档

25 篇文章 1 订阅
11 篇文章 0 订阅

swagger ui是一个API在线文档生成和测试的利器,目前发现最好用的。
为什么好用?支持API自动生成同步的在线文档, 这些文档可用于项目内部API审核,方便测试人员了解API,这些文档可作为客户产品文档的一部分进行发布,支持API规范生成代码,生成的客户端和服务器端骨架代码可以加速开发和测试速度.
不多说下面进行一下配置
首先引入pom 依赖

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

2.配置文件

@Configuration //必须存在
@EnableSwagger2 //必须存在
public class SwaggerConfig{
    @Bean
    public Docket demoApi() {
           return new Docket(DocumentationType.SWAGGER_2)
                   .apiInfo(getApiInfo()).select()
                   .apis(RequestHandlerSelectors.basePackage("com.wen.security.controller"))
                   .paths(PathSelectors.any())
                   .build();
    }

    protected ApiInfo getApiInfo()
    {
      return new ApiInfo("Rest Web Service", "cxhc Rest Web Service " + new Date(), "", "",
          new Contact("cxhc", "", ""), "", "",new ArrayList<VendorExtension>());
    }
}

3.添加方法和参数描述

    @GetMapping("/{id}")
    @ApiOperation(value = "普通线程", notes = "普通线程描述")
    public String testSwagger(@ApiParam(name = "id", value = "编号", required = true) @PathVariable int id) {
        logger.info("主线程开始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("主线程结束");
        return "success";
    }

展示效果如下
这里写图片描述
4.注解描述

@ApiIgnore

忽略暴露的 api

@ApiOperation(value = “查找”, notes = “根据用户 ID 查找用户”)

添加说明

@Api :用在类上,说明该类的作用

@ApiImplicitParams :用在方法上包含一组参数说明

@ApiResponses :用于表示一组响应

@ApiResponse :用在@ApiResponses 中,一般用于表达一个错误的响应信息

code:数字,例如 400

message:信息,例如”请求参数没填好”

response:抛出异常的类

@ApiModel :描述一个 Model 的信息(这种一般用在 post 创建的时候,使用@RequestBody 这样的场景,请求参数无法使用@ApiImplicitParam 注解进行描述的时候)

@ApiModelProperty :描述一个 model 的属性

文章地址:http://www.haha174.top/article/details/254541
项目源码:https://github.com/haha174/imooc-security.git

欢迎关注,更多福利

这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值