后端开发中经常需要对移动客户端提供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文档的更多相关文章

  1. Spring Boot中使用Swagger2构建API文档

    程序员都很希望别人能写技术文档,自己却很不愿意写文档.因为接口数量繁多,并且充满业务细节,写文档需要花大量的时间去处理格式排版,代码修改后还需要同步修改文档,经常因为项目时间紧等原因导致文档滞后于代码 ...

  2. springboot利用swagger构建api文档

    前言 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容,见参考资料. S ...

  3. Spring Boot 整合Swagger2构建API文档

    1.pom.xml中引入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

  4. springboot+mybatis-puls利用swagger构建api文档

    项目开发常采用前后端分离的方式.前后端通过API进行交互,在Swagger UI中,前后端人员能够直观预览并且测试API,方便前后端人员同步开发. 在SpringBoot中集成swagger,步骤如下 ...

  5. SpringBoot+rest接口+swagger2生成API文档+validator+mybatis+aop+国际化

    代码地址:JillWen_SpringBootDemo mybatis 1. 添加依赖: <dependency> <groupId>org.mybatis.spring.bo ...

  6. springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验--异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档---jpa访问数据库及page进行分页---整合redis---定时任务

    springboot学习-jdbc操作数据库--yml注意事项--controller接受参数以及参数校验-- 异常统一管理以及aop的使用---整合mybatis---swagger2构建api文档 ...

  7. 白话SpringCloud | 第十一章:路由网关(Zuul):利用swagger2聚合API文档

    前言 通过之前的两篇文章,可以简单的搭建一个路由网关了.而我们知道,现在都奉行前后端分离开发,前后端开发的沟通成本就增加了,所以一般上我们都是通过swagger进行api文档生成的.现在由于使用了统一 ...

  8. 使用springfox+swagger2书写API文档(十八)

    使用springfox+swagger2书写API文档 springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfo ...

  9. Spring Boot中使用Swagger2自动构建API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

随机推荐

  1. zip unzip 压缩解压缩命令

    直接上例子: mkdir test1 touch test1/1.txt touch test1/2.txt zip -r test1.zip test1    #-r 参数是包含文件夹下的文件 un ...

  2. Matplotlib基础 可视化绘图 学习笔记

    简单的绘图 1.确定画布并画线 import matplotlib.pyplot as plt #静态绘图 fig = plt.figure() ax = fig.add_subplot(345) # ...

  3. postman---postman发送请求

    前面简单的介绍了Postman的页面介绍和功能介绍,今天我们一起学习postman如何发送请求 发送请求 我们介绍过http协议有多种请求方式,各个请求方法都代表不同的结果.例如,GET使您可以从服务 ...

  4. client-go集群外认证k8s

    除了认证外,还判断了操作系统. 且根据不同的系统,生成不同的文件. 在集群外认证时,使用的是k8s官方的方式, 而不是第三方库. package main import ( "flag&qu ...

  5. 《为什么说Redis是单线程的以及Redis为什么这么快!》

    为什么说Redis是单线程的以及Redis为什么这么快!   一.前言 近乎所有与Java相关的面试都会问到缓存的问题,基础一点的会问到什么是“二八定律”.什么是“热数据和冷数据”,复杂一点的会问到缓 ...

  6. polyfll方案优化

    polyfill 在es6风靡的时候,babel给了我们一个有力的转换方案,可以在低版本浏览器上写一些新语法而不用考虑兼容问题 polyfill的诞生 语法和API区分 语法是用来产生特殊效果的一些符 ...

  7. [日常] SNOI2019场外VP记

    SNOI2019场外VP记 教练突然说要考一场别省省选来测试水平...正好还没看题那就当VP咯w... Day 1 八点开题打 .vimrc. 先看了看题目名...一股莫名鬼畜感袭来... 怎么T1就 ...

  8. vue_03day

    目录 作业: vue组件操作页面渲染: 组件渲染: 作业: vue组件操作页面渲染: 1.有以下广告数据(实际数据命名可以略做调整) ad_data = { tv: [ {img: 'img/tv/0 ...

  9. [算法模版]Tarjan爷爷的几种图论算法

    [算法模版]Tarjan爷爷的几种图论算法 前言 Tarjan爷爷发明了很多图论算法,这些图论算法有很多相似之处(其中一个就是我都不会).这里会对这三种算法进行简单介绍. 定义 强连通(strongl ...

  10. COMP222 - 2019

    COMP222 - 2019 - Second CA AssignmentIndividual courseworkTrain Deep Learning AgentsAssessment Infor ...