现代化的研发组织架构中,一个研发团队基本包括了产品组、后端组、前端组、APP端研发、测试组、UI组等,各个细分组织人员各司其职,共同完成产品的全周期工作。如何进行组织架构内的有效高效沟通就显得尤其重要。其中,如何构建一份合理高效的接口文档更显重要。

接口文档横贯各个端的研发人员,但是由于接口众多,细节不一,有时候理解起来并不是那么容易,引起‘内战’也在所难免, 并且维护也是一大难题。

类似RAP文档管理系统,将接口文档进行在线维护,方便了前端和APP端人员查看进行对接开发,但是还是存在以下几点问题:

  • 文档是接口提供方手动导入的,是静态文档,没有提供接口测试功能;
  • 维护的难度不小。

Swagger的出现可以完美解决以上传统接口管理方式存在的痛点。本文介绍Spring Boot整合Swagger2的流程,连带填坑。

使用流程如下:

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>
复制代码

2)编写Swagger2的配置类:

package com.trace.configuration;import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;/**
* Created by Trace on 2018-05-16.<br/>
* Desc: swagger2配置类
*/
@SuppressWarnings({"unused"})
@Configuration @EnableSwagger2
public class Swagger2Config {@Value("${swagger2.enable}") private boolean enable;@Bean("UserApis")public Docket userApis() {return new Docket(DocumentationType.SWAGGER_2).groupName("用户模块").select().apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.regex("/user.*")).build().apiInfo(apiInfo()).enable(enable);}@Bean("CustomApis")public Docket customApis() {return new Docket(DocumentationType.SWAGGER_2).groupName("客户模块").select().apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.regex("/custom.*")).build().apiInfo(apiInfo()).enable(enable);}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("XXXXX系统平台接口文档").description("提供子模块1/子模块2/子模块3的文档, 更多请关注公众号: 随行享阅").termsOfServiceUrl("https://xingtian.github.io/trace.github.io/").version("1.0").build();}
}复制代码

如上可见:

  • 通过注解@EnableSwagger2开启swagger2,apiInfo是接口文档的基本说明信息,包括标题、描述、服务网址、联系人、版本等信息;
  • 在Docket创建中,通过groupName进行分组,paths属性进行过滤,apis属性可以设置扫描包,或者通过注解的方式标识;通过enable属性,可以在application-{profile}.properties文件中设置相应值,主要用于控制生产环境不生成接口文档。

3)controller层类和方法添加相关注解

package com.trace.controller;import com.trace.bind.ResultModel;
import com.trace.entity.po.Area;
import com.trace.entity.po.User;
import com.trace.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;/*** Created by Trace on 2017-12-01.<br/>* Desc: 用户管理controller*/
@SuppressWarnings("unused")
@RestController @RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {@Resource private UserService userService;@GetMapping("/query/{id}")@ApiOperation("通过ID查询")@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path")public ResultModel<User> findById(@PathVariable int id) {User user = userService.findById(id);return ResultModel.success("id查询成功", user);}@GetMapping("/query/ids")@ApiOperation("通过ID列表查询")public ResultModel<List<User>> findByIdIn(int[] ids) {List<User> users = userService.findByIdIn(ids);return ResultModel.success("in查询成功", users);}@GetMapping("/query/user")@ApiOperation("通过用户实体查询")public ResultModel<List<User>> findByUser(User user) {List<User> users = userService.findByUser(user);return ResultModel.success("通过实体查询成功", users);}@GetMapping("/query/all")@ApiOperation("查询所有用户")public ResultModel<List<User>> findAll() {List<User> users = userService.findAll();return ResultModel.success("全体查找成功", users);}@GetMapping("/query/username")@ApiOperation("通过用户名称模糊查询")@ApiImplicitParam(name = "userName", value = "用户名称")public ResultModel<List<User>> findByUserName(String userName) {List<User> users = userService.findByUserName(userName);return ResultModel.success(users);}@PostMapping("/insert")@ApiOperation("新增默认用户")public ResultModel<Integer> insert() {User user = new User();user.setUserName("zhongshiwen");user.setNickName("zsw");user.setRealName("钟仕文");user.setPassword("zsw123456");user.setGender("男");Area area = new Area();area.setLevel((byte) 5);user.setArea(area);userService.save(user);return ResultModel.success("新增用户成功", user.getId());}@PutMapping("/update")@ApiOperation("更新用户信息")public ResultModel<Integer> update(User user) {int row = userService.update(user);return ResultModel.success(row);}@PutMapping("/update/status")@ApiOperation("更新单个用户状态")@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", required = true),@ApiImplicitParam(name = "status", value = "状态", required = true)})public ResultModel<User> updateStatus(int id, byte status) {User user = userService.updateStatus(id, status);return ResultModel.success(user);}@DeleteMapping("/delete")@ApiOperation("删除单个用户")@ApiImplicitParam(value = "用户ID", required = true)public ResultModel<Integer> delete(int id) {return ResultModel.success(userService.delete(id));}
}复制代码


4)返回对象ResultModel

package com.trace.bind;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;/**
* Created by Trace on 2017-12-01.<br/>
* Desc:  接口返回结果对象
*/
@SuppressWarnings("unused")
@Getter @Setter @ApiModel(description = "返回结果")
public final class ResultModel<T> {@ApiModelProperty("是否成功: true or false")private boolean result;@ApiModelProperty("描述性原因")private String message;@ApiModelProperty("业务数据")private T data;private ResultModel(boolean result, String message, T data) {this.result = result;this.message = message;this.data = data;}public static<T> ResultModel<T> success(T data) {return new ResultModel<>(true, "SUCCESS", data);}public static<T> ResultModel<T> success(String message, T data) {return new ResultModel<>(true, message, data);}public static ResultModel failure() {return new ResultModel<>(false, "FAILURE", null);}public static ResultModel failure(String message) {return new ResultModel<>(false, message, null);}
}
复制代码


5)ApiModel属性对象 -- User实体

package com.trace.entity.po;import com.trace.mapper.base.NotPersistent;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;/*** Created by Trace on 2017-12-01.<br/>* Desc: 用户表tb_user*/
@SuppressWarnings("unused")
@Data @NoArgsConstructor @AllArgsConstructor
@ApiModel
public class User {@ApiModelProperty("用户ID") private Integer id;@ApiModelProperty("账户名") private String userName;@ApiModelProperty("用户昵称") private String nickName;@ApiModelProperty("真实姓名") private String realName;@ApiModelProperty("身份证号码") private String identityCard;@ApiModelProperty("性别") private String gender;@ApiModelProperty("出生日期") private LocalDate birth;@ApiModelProperty("手机号码") private String phone;@ApiModelProperty("邮箱") private String email;@ApiModelProperty("密码") private String password;@ApiModelProperty("用户头像地址") private String logo;@ApiModelProperty("账户状态 0:正常; 1:冻结; 2:注销") private Byte status;@ApiModelProperty("个性签名") private String summary;@ApiModelProperty("用户所在区域码") private String areaCode;@ApiModelProperty("注册时间") private LocalDateTime registerTime;@ApiModelProperty("最近登录时间") private LocalDateTime lastLoginTime;@NotPersistent @ApiModelProperty(hidden = true)private transient Area area; //用户所在地区@NotPersistent @ApiModelProperty(hidden = true)private transient List<Role> roles; //用户角色列表
}
复制代码

简单说下Swagger2几个重要注解:

@Api:用在请求的类上,表示对类的说明

  • tags "说明该类的作用,可以在UI界面上看到的注解"
  • value "该参数没什么意义,在UI界面上也看到,所以不需要配置"

@ApiOperation:用在请求的方法上,说明方法的用途、作用

  • value="说明方法的用途、作用"
  • notes="方法的备注说明"

@ApiImplicitParams:用在请求的方法上,表示一组参数说明

@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面

  • value:参数的汉字说明、解释
  • required:参数是否必须传
  • paramType:参数放在哪个地方 
    1. header --> 请求参数的获取:@RequestHeader
    2. query --> 请求参数的获取:@RequestParam
    3. path(用于restful接口)--> 请求参数的获取:@PathVariable
    4. body(不常用)
    5. form(不常用)
  • dataType:参数类型,默认String,其它值dataType="Integer"
  • defaultValue:参数的默认值

@ApiResponses:用在请求的方法上,表示一组响应

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

  • code:数字,例如400
  • message:信息,例如"请求参数没填好"
  • response:抛出异常的类

@ApiModel:主要有两种用途:

  • 用于响应类上,表示一个返回响应数据的信息
  • 入参实体:使用@RequestBody这样的场景, 请求参数无法使用@ApiImplicitParam注解进行描述的时候

@ApiModelProperty:用在属性上,描述响应类的属性

最终呈现结果:

如前所述:通过maven导入了swagger-ui:

<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version>
</dependency>
复制代码

那么,启动应用后,会自动生成http://{root-path}/swagger-ui.html页面,访问后,效果如下所示:

可以在线测试接口,如通过ID查询的接口/user/query/{id}

全文完!

下一篇:Java效率工具之Lombok

Java效率工具之Swagger2相关推荐

  1. Java 效率工具 Lombok 使用教程

    来源:微信公众号 → JavaGuide → 2019/06/17 → https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=224748 ...

  2. 代码洁癖的春天!Java 效率工具之 Lombok

    点击上方"搜云库技术团队",选择"设为星标" 回复"1024"或"面试题"获取学习资料 还在编写无聊枯燥又难以维护的PO ...

  3. Java 效率工具之 Lombok

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:用好Java中的枚举,真的没有那么简单!个人原创+1博客:点击前往,查看更多 作者:LiWenD正在掘金 链接: ...

  4. Java:效率工具之随机号码生成器

    有时候做数据库测试时,号码段需要一些数据填充,如果手动去填写,未免太慢,该java程序可以帮助你快速批量生成随机号码 package com.test;public class Suijihaoma4 ...

  5. java myeclipse 下载_myeclipse 10|MyEclipse(优秀的Java开发工具myeclipse下载) 10.7官方版下载 - 下载吧...

    MyEclipse10官方下载是一款非常优秀的Java开发工具.MyEclipse的功能非常强大,支持也十分广泛,尤其是对各种开源产品的支持十分不错.MyEclipse目前支持Java Servlet ...

  6. java 开发工具_Java开发工具和环境,你了解多少?

    Java作为今年来最热门的编程语言之一,越来越多的人选择Java,但对于一些初入门的小白来说,在选择和安装开发工具和环境的时候,会遇见很多的问题. 今天就给大家来分享一些实用的Java开发工具和环境, ...

  7. 高效Java编程工具集锦

    Java 开发者常常都会想办法如何更快地编写 Java 代码,让编程变得更加轻松.目前,市面上涌现出越来越多的高效编程工具.所以,以下总结了一系列工具列表,其中包含了大多数开发人员已经使用.正在使用或 ...

  8. 十四种Java开发工具点评

    图形界面的java开发工具 JDK Borland 的JBuilder ,JDeveloper,VisualAge for Java jcreater. 常见的十五种Java开发工具的特点 1.JDK ...

  9. 1 java开发工具IDEA的使用

    IntelliJ IDEA 2017.1汉化破解版安装图文教程(附汉化补丁) 注册码:http://idea.lanyus.com/  点击在线生成 IntelliJ IDEA 2017.1正式版发布 ...

最新文章

  1. 机器学习拓展知识(数学/统计/算法)
  2. 零基础入门学习Python,这13个Python惯用小技巧一定要收藏
  3. adb devices unauthorized解决方法
  4. 【Python之路】第五篇--Python基础之杂货铺
  5. U盘的挂载和卸载(也可以查看指令篇)
  6. python中mean的用法_Python statistics mean()用法及代码示例
  7. 关于FileSystemWatcher监听文件创建
  8. matplotlib 横坐标只显示整数_面试题系列 (168) matplotlib条形图绘制
  9. 高博的一起做RGB-D SLAM 简单总结的流程框图
  10. python stdout stderr 一起输出_Python日志记录在stdout和stderr之间拆分
  11. OpenMP和Pthread比较
  12. ANSYS网格划分---单元类型选择及步骤
  13. 2018年的好书基本都在这了,你一共读过几本?
  14. Android 模拟器 连接局域网
  15. Windows安装 choco
  16. 「模拟8.19 A嚎叫..(set) B主仆..(DFS) C征程..(DP+堆优化)」
  17. android:kotlin语言开发再也不用findViewById与ButterKnife
  18. 如何给MFC对话框添加背景图片
  19. 【AE工具】AE一键切换中英文小工具,免费下载 支持CC2014-CC2019
  20. Hibernate 3.6.10 jar包下载链接

热门文章

  1. J2ME游戏中的图片处理
  2. 找出没有出现的数 题解
  3. Google的电话面试
  4. python 发邮件_Python发邮件告别smtplib,迎接zmail
  5. httos双向认证配置_idou老师教你学Istio 15:Istio实现双向TLS的迁移
  6. 目标检测如何计算召回率_计算机视觉目标检测的框架与过程
  7. mysql 从库 问题_一篇文章帮你解决Mysql 中主从库不同步的问题
  8. python如何全网爬取_如何通过Python爬取互联网
  9. 聚类中心坐标公式如何使用_如何使用CAD看图软件来测量坐标?
  10. x光肺部分割数据集_吴恩达发布了大型X光数据集,斯坦福AI诊断部分超越人类 | AAAI 2019...