SwaggerUI 自动生成文档

  • Post author:
  • Post category:其他


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

欢迎关注,更多福利

这里写图片描述



版权声明:本文为u012957549原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。