编写接口文档是一个非常枯燥的工作,我们采用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. java 深copy

    public static<T> T deepClone(T src) throws IOException, ClassNotFoundException { Object obj = ...

  2. String s = new String("xyz");创建了几个String Object?并作说明。

    String s = new String("xyz");创建了几个String Object?并作说明. 共产生了两个2个对象,第一个是字符串常量xyz,存储于常量池中.第二个对 ...

  3. pointnet

    无序性:虽然输入的点云是有顺序的,但是显然这个顺序不应当影响结果.点之间的交互:每个点不是独立的,而是与其周围的一些点共同蕴含了一些信息,因而模型应当能够抓住局部的结构和局部之间的交互.变换不变性:比 ...

  4. UFUN函数 UF_UI UF_PART函数(UF_UI_select_with_class_dialog, UF_PART_export_with_options)

    /*主要演示 UF_PART_export_with_options 这个函数 */1 //设置class_dialog选择过滤 static int init_proc(UF_UI_selectio ...

  5. wpf, C# 实现目录对话框选择

    引用:using System.Windows.Forms; 代码: FolderBrowserDialog fbWnd = new FolderBrowserDialog(); fbWnd.Desc ...

  6. 根据 oracle 标准计算超长字符串的长度

    Oracle 数据库使用 sql语句 :    select lengthb('输入字符串') from dual  ,  来计算 字符串 所占的字节长度(比如,一个汉字3个字节),但是用这个leng ...

  7. [cf113d]Museum

    传送门 Solution 设一个状态为 \((x,y)\) 表示两人在的位置,求出每个状态期望出现的次数 设一个状态为 \(u\) , \(x_u^0=[u==(a,b)]\) 所以一个状态出现的次数 ...

  8. 【cf补题记录】Codeforces Round #608 (Div. 2)

    比赛传送门 再次改下写博客的格式,以锻炼自己码字能力 A. Suits 题意:有四种材料,第一套西装需要 \(a\).\(d\) 各一件,卖 \(e\) 块:第二套西装需要 \(b\).\(c\).\ ...

  9. AppDomin学习与分享

    最近学习并分享了appdomin的一些东西,以前没怎么记录过,现在记录一下吧2016-03-17 什么是AppDomin •全称:Application Domin(应用程序域) •定义:AppDom ...

  10. Sc config http start= disabled

    我不小心使用这个命令 Sc config http start= disabled, 现在http服务 无法启动 管理员运行 sc config http start= demand & ne ...