编写接口文档是一个非常枯燥的工作,我们采用Swagger2这套自动化文档工具来生成文档,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档。

1、在pom.xml文件中加入Swagger2的依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

2、创建Swagger2配置类

 package com.offcn.springbootdemo2.Config;

 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; @Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select().apis(RequestHandlerSelectors.basePackage("com.offcn.springbootdemo2.Controller"))
.paths(PathSelectors.any()).build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("RestFul风格").description("一种好的习惯")
.termsOfServiceUrl("http://www.baidu.com").contact("java").version("1.0").build();
}
}

3、修改Controller增加文档注释

 package com.offcn.springbootdemo2.Controller;

 import com.offcn.springbootdemo2.Pojo.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; @RestController
@RequestMapping("/user")
public class UserController { private List<User> listUser= Collections.synchronizedList(new ArrayList<User>());
@GetMapping("/")
public List<User> getUserList(){
return listUser;
} /***
* 新增用户
* @param user
* @return
*/
@PostMapping("/")
public String createUser(User user) {
listUser.add(user);
return "success";
} /***
* 获取指定id用户信息
* @param id
* @return
*/
@GetMapping("/{id}") public User getUser(@PathVariable("id") Integer id) {
for (User user : listUser) {
if(user.getId()==id) {
return user;
}
}
return null;
}
/**
* 更新指定id用户信息
* @param id
* @param user
* @return
*/
@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 updateUser(@PathVariable("id") Integer id,User user) {
for (User user2 : listUser) {
if(user2.getId() == id) {
user2.setName(user.getName());
user2.setAge(user.getAge());
}
}
return "success";
} /***
* 删除指定id用户
* @param id
* @return
*/
@DeleteMapping("/{id}")
@ApiOperation(value="删除指定id用户信息", notes="根据id删除用户信息")
@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
public String deleteUser(@PathVariable("id") Integer id) { listUser.remove(getUser(id));
return "success"; }
}

4、启动项目,在浏览器地址输入

http://localhost:8080/swagger-ui.html

查看Swagger2文档

额外添加一个依赖Lombok,可以省略实体类中繁琐的代码

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>

Swgger2的简单使用的更多相关文章

  1. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  2. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  3. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  4. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  5. 在Openfire上弄一个简单的推送系统

    推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...

  6. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  7. 使用 Nodejs 搭建简单的Web服务器

    使用Nodejs搭建Web服务器是学习Node.js比较全面的入门教程,因为要完成一个简单的Web服务器,你需要学习Nodejs中几个比较重要的模块,比如:http协议模块.文件系统.url解析模块. ...

  8. ASP.NET Aries 入门开发教程2:配置出一个简单的列表页面

    前言: 朋友们都期待我稳定地工作,但创业公司若要躺下,也非意念可控. 若人生注定了风雨飘摇,那就雨中前行了. 最机开始看聊新的工作机会,欢迎推荐,创业公司也可! 同时,趁着自由时间,抓紧把这系列教程给 ...

  9. 简单入门canvas - 通过刮奖效果来学习

    一 .前言 一直在做PC端的前端开发,从互联网到行业软件.最近发现移动端已经成为前端必备技能了,真是不能停止学习.HTML5新增的一些东西,canvas是用的比较多也比较复杂的一个,简单的入门了一下, ...

随机推荐

  1. flask的客户端服务端

    1.首先要进行后端与前端的连接有get 和post请求 get请求是直接在网页上打出已将定义好的网址 if __name__ == '__main__': app.run(host="loc ...

  2. sublime python 配置内容

    {"cmd": ["python", "-u", "$file"],"file_regex": &q ...

  3. jsp获取map

    1.简单Map User user = new User(); user.setName("zmy"); user.setAge(); user.setBirthday(new D ...

  4. 出现 sudo: unable to resolve host XXX 信息解决办法

    Ubuntu环境,  每次执行sudo 就出现这个警告讯息:sudo: unable to resolve host XXX虽然sudo 还是可以正常执行,是机器在反解上的问题, 所以就直接从/etc ...

  5. 分布式环境配置虚拟域名,phpstudy配置虚拟域名,集成环境配置域名,域名禁止访问forbidden怎么解决

    重启Apache,测试:  

  6. 关于时间排序在ios中失效的处理方法

    上个月公司做项目的时候在列表排序的时候产品加了一个需求,通过点击量,发布时间,评论量进行筛选的一个需求. 一开始在电脑上测试基本没问题,然后我也就放下了这个按耐不住的小心脏,然后在完成所有模块后 sh ...

  7. django ORM CRUD

    一.增加数据-Create 1.类名.objects.create(属性=值,属性=值) Myomodel.objects.create(name=) 2.d={"属性":&quo ...

  8. vue+element 表单验证

    效果图 <template> <div class="formValidator"> <div v-for="(item,index) in ...

  9. [Gamma阶段]测试报告

    [Gamma阶段]测试报告 博客目录 测试方法及过程 在正式发布前,为检验后端各接口功能的正确性,后端服务器对压力的耐受程度,以及前端各页面.功能的运行情况,我们对我们的服务器及小程序进行了多种测试. ...

  10. 批处理中setlocal enabledelayedexpansion的作用详细整理

    转自:https://www.jb51.net/article/29323.htm 设置本地为延迟扩展.其实也就是:延迟变量,全称延迟环境变量扩展, 想进阶,变量延迟是必过的一关!所以这一部分希望你能 ...