昨天,经不住诱惑喝了一杯奶茶,果然又熬到了3点。熬夜是真的难受,睡不着,不睡起来看看书吧,脑子又不在线了。所以,在什么时间就做什么事情,还是很有道理的。有的人活着像是死了,有的人醒着,脑子却睡了。
另外,之前有了解过一些Swagger,那时候感觉用起来麻烦,还没有体会到方便之处。感悟:有些技术,学了就要落地到实处,学到位,用起来,慢慢就变成自己的本领了。
好了,进入正题。
目录
Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。简单来说,Swagger可以通过使用注解来生成接口文档,使得后端开发人员不必接口文档和代码各维护一套,代码即接口文档,代码更新接口文档可以自动更新。本篇文章主要讲解Springboot集成Swagger的配置,以及Swagger常用注解。 另外,Swagger也提供了根据接口文档生成前后端代码的功能,在此不做介绍。
1. Springboot集成Swagger
1.1 Maven项目引入依赖
<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>
1.2 Swagger的配置类
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
@Bean
public Docket buildDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xingguo.springboot.controller")) //controller类的包路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInf(){
return new ApiInfoBuilder()
.title("大标题")
.description("文档描述信息")
.termsOfServiceUrl("http://blog.csdn.net/myservice网址链接")
.contact(new Contact("anthor", "http://blog.csdn.net", "pxy@163.com"))
.version("1.0")
.build();
}
}
另外,swagger在生产环境不能对外暴露,可以使用@Profile({“dev”, “debug”, “test”})指定可以使用的环境
2. 常用注解
1. @Api,用在Controller类上;
- tags, 表示说明
- value, 也是说明,可以使用tags替代
@RestController
@Api(value = "描述信息", tags = { "描述此controller类" })
public class TestController {
...
}
2. @ApiOperation(),用在Controller类里面的方法,表示一个http请求;
- value, 用于方法描述
- notes, 用于提示内容
- tags, 可以重新分组(视情况而用)
3. @ApiParam, 用在方法参数上,表示对参数添加元数据(说明或是否必填等);
- name, 参数名
- value, 参数说明
- required, 是否必填
4. @ApiImplicitParam() , 用于方法, 表示单独的请求参数 ;
5. @ApiImplicitParams, 包含多个 @ApiImplicitParam;
- name, 参数名
- value, 参数说明
- dataType, 数据类型, 例如 string, int, ArrayList, POJO类
- paramType, 参数类型 , 此属性的可选值 ["header", "query", "path", "body", "form"]
- example, 举例说明
关于参数注解:
我的使用习惯:直接使用@ApiParam(value="描述",required=true)标注在请求参数上,参数类型(query/path/body)是可以自动分辨的。
另外如果觉得参数较多想要解耦,也可以使用@ApiImplicitParams将所有参数聚合标注在方法上;
6. @ApiResponses, 用于方法上,包含多个 @ApiResponse,枚举接口响应结果;
@PostMapping("/api/v1/user")
@ApiOperation(value = "方法描述 ", notes="提示内容,注意问题点")
@ApiImplicitParams({@ApiImplicitParam(name="name(参数名)",value="用户名(参数描述)",dataType="string(数据类型)", paramType = "query(参数类型)",example="pxy(示例值)"),
@ApiImplicitParam(name="id",value="用户id",dataType="long", paramType = "query")})
@ApiResponses(value = { @ApiResponse(code = 400, message = "操作失败"),
@ApiResponse(code = 500, message = "系统内部错误"),
@ApiResponse(code = 200, response = User.class, message = "成功") })
public User testUser(@ApiParam(name="id(参数名)", value="用户id(参数说明)", required=true(是否必填)) @RequestParam Long id, @RequestBody @ApiParam(name="user", value="传入json体", required=true) User user){
...
}
7. @ApiModel(), 用于请求类或响应类;
- value,表示对象名
- description,描述
8. @ApiModelProperty(),用于model类属性,表示对model属性的说明
- value, 字段说明
- name, 重写属性名字
- dataType, 重写属性类型
- required, 是否必填
- example, 举例说明
- hidden, 隐藏
@ApiModel(value="user对象",description="用户对象user")
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@ApiModelProperty(value="用户名",name="username",example="pxy")
private String username;
@ApiModelProperty(value="状态",name="state",required=true)
private Integer state;
private String password;
private String nickName;
private Integer isDeleted;
@ApiModelProperty(value="id数组",hidden=true)
private String[] ids;
private List<String> idList;
//省略get/set
}
9. @ApiIgnore(), 用于类,方法,方法参数 , 表示这个方法或者类被忽略 ;
3. 访问
Swagger UI接口文档,默认的访问地址:http://ip:port/swagger-ui.html
参考:官方文档