SpringBoot使用Swagger2构建API文档
后端开发中经常需要对移动客户端提供RESTful API接口,在后期版本快速迭代的过程中,修改接口实现的时候都必须同步修改接口文档,而文档与代码又处于两个不同的媒介,除非有严格的管理机制,不然很容易导致写出的代码与接口文档不一致现象。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。


1.在pom.xml中加入swagger2依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
2.创建swagger2配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.offcn.controller"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("描述描述")
.termsOfServiceUrl("http://www.baidu.com/")
.contact("Sunny")
.version("1.0")
.build();
}
}
3.Controller增加文档注释
通过@ApiImplicitParams@ApiImplicitParam注解来给参数增加说明
@RestController
@EnableConfigurationProperties({User.class})
@RequestMapping("/user")
public class UserController { @Autowired
User user; private List<User> listUser= Collections.synchronizedList(new ArrayList<User>()); //查询所有
@GetMapping("/")
@ApiOperation(value="查询All用户信息", notes="All用户信息")
public List<User> getAllUser(){
return listUser;
}
//获取指定id的user
@GetMapping("/{id}")
@ApiOperation(value="查询指定id用户信息", notes="根据id查用户信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
public User getUser(@PathVariable("id") Long id){
for(User u:listUser){
if(u.getId()==id){
return user;
}
}
return null;
}
//插入
@PostMapping("/")
@ApiOperation(value="插入用户", notes="插入用户信息")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
public String insert(User user){
listUser.add(user);
return "success";
}
//根据id修改
@PutMapping("/{id}")
@ApiOperation(value="更新指定id用户信息", notes="根据id更新用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
})
public String update(@PathVariable("id") Long id,User user){
for (User user2 : listUser) {
if(user2.getId()==id) {
user2.setName(user.getName());
user2.setAge(user.getAge());
}
}
return "success";
}
//根据id删除
@DeleteMapping("/{id}")
@ApiOperation(value="删除指定id用户信息", notes="根据id删除用户信息")
@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
public String delete(@PathVariable("id") Long id){
listUser.remove(getUser(id));
return "success";
} }
4.启动tomcat:
访问地址:http://localhost:8080/swagger-ui.html
即可看到上面的API
SpringBoot使用Swagger2构建API文档的更多相关文章
- Spring Boot中使用Swagger2构建API文档
程序员都很希望别人能写技术文档,自己却很不愿意写文档.因为接口数量繁多,并且充满业务细节,写文档需要花大量的时间去处理格式排版,代码修改后还需要同步修改文档,经常因为项目时间紧等原因导致文档滞后于代码 ...
- springboot利用swagger构建api文档
前言 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容,见参考资料. S ...
- Spring Boot 整合Swagger2构建API文档
1.pom.xml中引入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...
- springboot+mybatis-puls利用swagger构建api文档
项目开发常采用前后端分离的方式.前后端通过API进行交互,在Swagger UI中,前后端人员能够直观预览并且测试API,方便前后端人员同步开发. 在SpringBoot中集成swagger,步骤如下 ...
- SpringBoot+rest接口+swagger2生成API文档+validator+mybatis+aop+国际化
代码地址:JillWen_SpringBootDemo mybatis 1. 添加依赖: <dependency> <groupId>org.mybatis.spring.bo ...
- springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验--异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档---jpa访问数据库及page进行分页---整合redis---定时任务
springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验-- 异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档 ...
- 白话SpringCloud | 第十一章:路由网关(Zuul):利用swagger2聚合API文档
前言 通过之前的两篇文章,可以简单的搭建一个路由网关了.而我们知道,现在都奉行前后端分离开发,前后端开发的沟通成本就增加了,所以一般上我们都是通过swagger进行api文档生成的.现在由于使用了统一 ...
- 使用springfox+swagger2书写API文档(十八)
使用springfox+swagger2书写API文档 springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfo ...
- Spring Boot中使用Swagger2自动构建API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
随机推荐
- 6.3 使用Spark SQL读写数据库
Spark SQL可以支持Parquet.JSON.Hive等数据源,并且可以通过JDBC连接外部数据源 一.通过JDBC连接数据库 1.准备工作 ubuntu安装mysql教程 在Linux中启动M ...
- lua 5 流程控制 if
条件判断中,0 表示 true,只有 nil 才是 false if(0) then -- 可以没有括号 print("0 为 true") elseif 1 then print ...
- Http响应乱码
Http响应乱码 方案1 response.setHeader("Content-Type", "application/json"); response.se ...
- QTP10启动错误:Error creatingUnable to create configuration directory "C:UsersmR?ã? directory entry
安装完之后打开就一直: 百度也不知道为哈子(莫非是中文的用户名?反正我不想重装系统),真是很气人. 我就直接创建了一个临时账户,登陆进去,就可以运行了:
- 第05组团队Github现场编程实战
第05组团队Github现场编程实战 一.组员职责分工 组员 分工 卢欢(组长) 前后端接口设计 严喜 寻找相关资料 张火标 设计并描述界面原型 钟璐英 编写随笔 周华 填写完善文档 古力亚尔·艾山 ...
- polyfll方案优化
polyfill 在es6风靡的时候,babel给了我们一个有力的转换方案,可以在低版本浏览器上写一些新语法而不用考虑兼容问题 polyfill的诞生 语法和API区分 语法是用来产生特殊效果的一些符 ...
- 线性结构之习题选讲-ReversingLinkedList
目录 一.什么是抽象的链表 二.单链表的逆转 三.测试数据 3.1 边界测试 更新.更全的<数据结构与算法>的更新网站,更有python.go.人工智能教学等着你:https://www. ...
- C++:Copy & Reference Count
浅拷贝.深拷贝 通常,我们会按如下方式书写拷贝构造函数: class LiF { public: LiF(int _lif = 0) : lif(_lif) {} // 默认构造函数 LiF(cons ...
- snappy 安装
1 下载snappywget https://kojipkgs.fedoraproject.org//vol/fedora_koji_archive00/packages/snappy/1.1.0/1 ...
- LibLog 类库 分析
前期思考: Microsoft.Logging 是否可用? 是否需要提供默认的 Logger 实现? 不需要.1,用户自己开启日志功能,设置开启属性,即可打印出相应的日志. LibLog 类库分析: ...