springboot 2.6 版本以上的要在配置文件(yml/ properties)里添加下面的配置信息
1 2 3
| spring: mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER
|
一、常用注解
Swagger的所有注解定义在io.swagger.annotations包下
下面列一些经常用到的,未列举出来的可以另行查阅说明:
Swagger注解 |
简单说明 |
@Api(tags = “xxx模块说明”) |
作用在模块类上 |
@ApiOperation(“xxx接口说明”) |
作用在接口方法上 |
@ApiModel(“xxxPOJO说明”) |
作用在模型类上:如VO、BO |
@ApiModelProperty(value = “xxx属性说明”,hidden = true) |
作用在类方法和属性上,hidden设置为true可以隐藏该属性 |
@ApiParam(“xxx参数说明”) |
作用在参数、方法和字段上,类似@ApiModelProperty |
二、 依赖
1 2 3 4 5 6 7 8 9 10 11
| <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>
|
三、 配置类SwaggerConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| @Configuration @EnableSwagger2 public class SwaggerConfig {
@Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(true) .groupName("hello") .select()
.build(); }
private ApiInfo apiInfo() { Contact contact = new Contact("蓝", "https://lan-wa.github.io/", "1584476784@qq.com"); return new ApiInfo( "SwaggerAPI文档", "即使在小的帆也能远航", "v1.0", "https://lan-wa.github.io/", contact, "Apach 2.0 许可", "", new ArrayList<>() ); }
}
|
四、 实体配置
1、新建一个实体类:
1 2 3 4 5 6 7
| @ApiModel("用户实体") public class User { @ApiModelProperty("用户名") public String username; @ApiModelProperty("密码") public String password; }
|
2 、只要这个实体在请求接口的返回值上(即使是泛型),都能映射到实体项中:
1 2 3 4
| @RequestMapping("/getUser") public User getUser(){ return new User(); }
|
注:并不是因为@ApiModel这个注解让实体显示在这里了,而是只要出现在接口方法的返回值上的实体都会显示在这里,而@ApiModel和@ApiModelProperty这两个注解只是为实体添加注释的。
@ApiModel为类添加注释
@ApiModelProperty为类属性添加注释
我们也可以给请求的接口配置一些注释
1 2 3 4 5 6
| @ApiOperation("**的接口") @PostMapping("/user") @ResponseBody public String kuang(@ApiParam("这个名字会被返回")String username){ return username; }
|
这样的话,可以给一些比较难理解的属性或者接口,增加一些配置信息,让人更容易阅读!
相较于传统的Postman或Curl方式测试接口,使用swagger简直就是傻瓜式操作,不需要额外说明文档(写得好本身就是文档)而且更不容易出错,只需要录入数据然后点击Execute,如果再配合自动化框架,可以说基本就不需要人为操作了。
缺点:在正式环境要记得关闭Swagger,一来出于安全考虑二来也可以节省运行时内存。
重启项目,访问测试 http://localhost:8081/swagger-ui.html 看下效果;
五、拓展:其他皮肤
我们可以导入不同的包实现不同的皮肤定义:
1、默认的 访问 http://localhost:8080/swagger-ui.html
1 2 3 4 5
| <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
|
2、bootstrap-ui 访问 http://localhost:8080/doc.html
1 2 3 4 5 6
| <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.1</version> </dependency>
|
3、Layui-ui 访问 http://localhost:8080/docs.html
1 2 3 4 5 6
| <dependency> <groupId>com.github.caspar-chen</groupId> <artifactId>swagger-ui-layer</artifactId> <version>1.1.3</version> </dependency>
|
4、mg-ui 访问 http://localhost:8080/document.html
1 2 3 4 5 6
| <dependency> <groupId>com.zyplayer</groupId> <artifactId>swagger-mg-ui</artifactId> <version>1.0.6</version> </dependency>
|