Spring Boot 系列(七)Swagger2-生成RESTful接口文档
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。
开始
1、pom.xml 添加依赖:
<!-- swagger RESTful API 文档 -->
<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、创建 User 实体
package com.sam.demo.domain;
/**
* @author sam
* @since 2017/7/17
*/
public class User {
private Long id;
private String name;
private int age;
//getter & setter
}
3、在 Application.java 同级创建 Swagger2.java
package com.sam.demo;
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;
/**
* @author sam
* @since 2017/7/17
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
ApiInfo apiInfo = new ApiInfoBuilder()
.title("Sam 项目接口文档")
.description("Magical Sam 项目的接口文档,符合RESTful API。")
.version("1.0")
.build();
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage("com.sam.demo.controller")) //以扫描包的方式
.paths(PathSelectors.any())
.build();
}
}
其中 .apis(RequestHandlerSelectors.basePackage("com.sam.demo.controller")) 指定了以扫描包的方式进行,会把com.sam.demo.controller包下的controller都扫描到。
4、创建 UserController.java
package com.sam.demo.controller;
import com.sam.demo.domain.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author sam
* @since 2017/7/14
*/
@Api(value = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {
/**
* 获取单个用户
*
* @param id
* @return
*/
@ApiOperation(value = "获取单个用户", notes = "传入id获取单个用户")
// @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Long") //注意:paramType需要指定为path,不然不能正常获取
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String user(@ApiParam(value = "用户Id", required = true) @PathVariable Long id) {
return "user id :" + id;
}
/**
* 获取用户列表
*
* @return
*/
@ApiOperation(value = "获取用户列表", notes = "获取用户列表")
@RequestMapping(value = "", method = RequestMethod.GET)
public List list() {
List list = new ArrayList();
list.add("Sam1");
list.add("Sam2");
list.add("Sam3");
return list;
}
/**
* 新建用户
*
* @param user
* @return
*/
@ApiOperation(value = "新建用户", notes = "新建一个用户")
// @ApiImplicitParams({
//注意:paramType需要指定为body
// @ApiImplicitParam(name = "user", value = "用户数据", required = true, paramType = "body", dataType = "User")
// })
@RequestMapping(value = "", method = RequestMethod.POST)
public String create(@ApiParam(value = "用户数据", required = true) @RequestBody User user) {
System.out.println("user : " + user.getName() + " " + user.getAge());
return "success 新建user : " + user.getName() + " " + user.getAge();
}
/**
* 删除用户
*
* @return
*/
@ApiOperation(value = "删除用户", notes = "通过用户id删除用户")
@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Long")
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable Long id) {
System.out.println("删除用户:" + id);
return "success 删除user" + id;
}
/**
* 更新用户
*
* @return
*/
@ApiOperation(value = "更新用户", notes = "更新已存在用户")
@ApiImplicitParam(name = "user", value = "用户数据", required = true, paramType = "body", dataType = "User")
@RequestMapping(value = "", method = RequestMethod.PUT)
public String update(@RequestBody User user) {
System.out.println("更新用户:" + user.getId() + " " + user.getName() + " " + user.getAge());
return "success 更新user : " + user.getId() + " " + user.getName() + " " + user.getAge();
}
}
启动应用,浏览器访问:http://localhost:8080/swagger-ui.html
正常展示 api 接口文档界面,如下:

注意1:@ApiImplicitParam 和 @ApiParam 方式均能指定参数规则。
注意2:使用@ApiImplicitParam的时候,需要指定paramType。
附:Swagger2相关注解介绍
@Api:用在类上,说明该类的作用
@ApiOperation:用在方法上,说明方法的作用
@ApiImplicitParams:用在方法上包含一组参数说明
@ApiImplicitParam:用在 @ApiImplicitParams 注解中,指定一个请求参数的各个方面
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query -->请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(不常用)
· form(不常用)
name:参数名
dataType:参数类型
required:参数是否必须传
value:参数的意思
defaultValue:参数的默认值
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:描述一个model的属性
版权声明:本文为博主原创文章,转载请注明出处。
Spring Boot 系列(七)Swagger2-生成RESTful接口文档的更多相关文章
- Spring Boot中使用Swagger2生成RESTful API文档(转)
效果如下图所示: 添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!-- https://mvnrepository.com/artifact/io.springfox ...
- Spring Boot中使用Swagger2构建RESTful API文档
在开发rest api的时候,为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题: 1.由于接口众多,并且细 ...
- Swagger+Spring mvc生成Restful接口文档
简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...
- Spring Boot 集成Swagger2生成RESTful API文档
Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API. 使用Spring Boot可 ...
- springboot项目利用Swagger2生成在线接口文档
Swagger简介. Swagger2是一款restful接口文档在线生成和在线调试工具.很多项目团队利用Swagger自动生成接口文档,保证接口文档和代码同步更新.在线调试.简单地说,你可以利用这个 ...
- Spring Boot中使用Swagger2自动构建API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
- Spring Boot2配置Swagger2生成API接口文档
一.Swagger2介绍 前后端分离开发模式中,api文档是最好的沟通方式. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 及时性 (接 ...
- Spring MVC学习总结(9)——Spring MVC整合swagger自动生成api接口文档
Swagger 号称:世界最流行的API框架,官网:http://swagger.io/,Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总 ...
- Spring Boot 入门系列(二十二)使用Swagger2构建 RESTful API文档
前面介绍了如何Spring Boot 快速打造Restful API 接口,也介绍了如何优雅的实现 Api 版本控制,不清楚的可以看我之前的文章:https://www.cnblogs.com/zha ...
- Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档
1.添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!--swagger2--> <dependency> <groupId>io.spr ...
随机推荐
- Ajax基本语法
案例代码: $(function(){ $('#send').click(function(){ $.ajax({ type: "GET", url: "test.jso ...
- centos7.6 ssh远程链接配置
1.firewall增加22端口号 增加方式有两种,直接编辑firewall的public.xml增加 vi /etc/firewalld/zones/public.xml 进入后按i健光标移动到zo ...
- go 函数类型
在go中,函数也可以被当成数据类型 e.g:下面有两个函数,+.-,然后定义了一个函数类型FuncType1,然后对funcType1附于不同的函数,则funcType1就可以执行相应的函数 pack ...
- 11-jQuery的事件绑定和解绑
1.绑定事件 语法: bind(type,data,fn) 描述:为每一个匹配元素的特定事件(像click)绑定一个事件处理器函数. 参数解释: type (String) : 事件类型 data ( ...
- Object constraint language for code generation from activity models
一.基本信息 标题:Object Constraint Language for Code Generation from Activity Models 时间:2018 出版源:Informatio ...
- 最近一个dish项目的建设思考
系统通用能力的沉淀:a.核心模型的数据沉淀 b.通用服务能力的沉淀 ps1:以前重心主要放在了业务的抽象和通过设计模式来增加可复用的扩展性.局限在于,抽象的范围会被单个业务或者当前的业务所束缚,在更大 ...
- VSCode 必装的 10 个高效开发插件 --转
https://www.cnblogs.com/parry/p/vscode_top_ten_plugins.html
- CentOS 5 常见的configure error的解决方法
仅限于CentOS 5 configure: error: No curses/termcap library found 网上有的说法是: --with-named-curses-libs=/usr ...
- 抄一篇maven的备忘
标注下来源:http://www.trinea.cn/android/maven/ 由浅入深,主要介绍maven的用途.核心概念(Pom.Repositories.Artifact.Build Lif ...
- W3C标准和规范
W3C标准万维网联盟标准. 万维网联盟(外语缩写:W3C)标准不是某一个标准,而是一系列标准的集合.网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behav ...