IDEA+SpringBoot整合Swagger2创建API文档
------------恢复内容开始------------
1.创建SpringBoot项目

2.选择快捷方式创建springboot项目




3.工程文件树形图

4.pom.xml中导入Swagger依赖

代码如下:
1 <dependency>
2 <groupId>io.springfox</groupId>
3 <artifactId>springfox-swagger2</artifactId>
4 <version>2.9.2</version>
5 </dependency>
6 <dependency>
7 <groupId>io.springfox</groupId>
8 <artifactId>springfox-swagger-ui</artifactId>
9 <version>2.9.2</version>
10 </dependency>
5.配置Swagger信息

代码如下:
1 package com.example.dell;
2
3 import org.springframework.context.annotation.Bean;
4 import org.springframework.context.annotation.Configuration;
5 import springfox.documentation.builders.ApiInfoBuilder;
6 import springfox.documentation.builders.PathSelectors;
7 import springfox.documentation.builders.RequestHandlerSelectors;
8 import springfox.documentation.service.Contact;
9 import springfox.documentation.spi.DocumentationType;
10 import springfox.documentation.spring.web.plugins.Docket;
11 import springfox.documentation.swagger2.annotations.EnableSwagger2;
12
13 /**
14 * @program:swagger2-demo
15 * @description:swagger配置类
16 * @author:feixiang
17 * @create:2020/5/14
18 */
19
20 @Configuration
21 @EnableSwagger2
22 public class Swagger2Config {
23 @Bean
24 public Docket createRestApi() {
25 return new Docket(DocumentationType.SWAGGER_2)
26 .pathMapping("/")
27 .select()
28 .apis(RequestHandlerSelectors.basePackage("com.example.dell.web"))
29 .paths(PathSelectors.any())
30 .build().apiInfo(new ApiInfoBuilder()
31 .title("飞翔用接口自动化平台")
32 .description("飞翔独享")
33 .version("9.0")
34 .contact(new Contact("马化腾","blog.csdn.net","aaa@gmail.com"))
35 .license("The Apache License")
36 .licenseUrl("http://www.baidu.com")
37 .build());
38 }
39 }
创建接口
接下来就是创建接口了,Swagger2相关的注解其实并不多,而且很容易懂,下面我来分别向小伙伴们举例说明

代码如下
1 package com.example.dell.web;
2
3 import com.example.dell.pojo.User;
4 import io.swagger.annotations.ApiImplicitParam;
5 import io.swagger.annotations.ApiImplicitParams;
6 import io.swagger.annotations.ApiOperation;
7 import org.springframework.web.bind.annotation.*;
8 import io.swagger.annotations.Api;
9 import org.springframework.web.bind.annotation.PathVariable;
10 import org.springframework.web.bind.annotation.RequestBody;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RestController;
13
14
15
16 @RestController
17 @Api(tags = "用户管理相关接口")
18 @RequestMapping("/user")
19 public class UserController{
20 @PostMapping("/")
21 @ApiOperation("添加用户的接口")
22 @ApiImplicitParams({
23 @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "张三"),
24 @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "佛山", required = true)
25 }
26 )
27 public RespBean addUser(String username, @RequestParam(required = true) String address) {
28 return new RespBean();
29 }
30
31 @GetMapping("/")
32 @ApiOperation("根据id查询用户的接口")
33 @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "99", required = true)
34 public User getUserById(@PathVariable Integer id) {
35 User user = new User();
36 user.setId(id);
37 return user;
38 }
39 @PutMapping("/{id}")
40 @ApiOperation("根据id更新用户的接口")
41 public User updateUserById(@RequestBody User user) {
42 return user;
43 }
44 }
这里边涉及到多个API,我来向小伙伴们分别说明:
@Api注解可以用来标记当前Controller的功能。
@ApiOperation注解用来标记一个方法的作用。
@ApiImplicitParam注解用来描述一个参数,可以配置参数的中文含义,也可以给参数设置默认值,这样在接口测试的时候可以避免手动输入。
如果有多个参数,则需要使用多个@ApiImplicitParam注解来描述,多个@ApiImplicitParam注解需要放在一个@ApiImplicitParams注解中。
需要注意的是,@ApiImplicitParam注解中虽然可以指定参数是必填的,但是却不能代替@RequestParam(required = true),前者的必填只是在Swagger2框架内必填,抛弃了Swagger2,这个限制就没用了,所以假如开发者需要指定一个参数必填,@RequestParam(required = true)注解还是不能省略。
如果参数是一个对象(例如上文的更新接口),对于参数的描述也可以放在实体类中。例如下面一段代码:
1 package com.example.dell.pojo;
2
3 import io.swagger.annotations.ApiModelProperty;
4
5
6
7
8 public class User {
9 @ApiModelProperty(value = "用户id")
10 private Integer id;
11 @ApiModelProperty(value = "用户名")
12 private String username;
13 @ApiModelProperty(value = "用户地址")
14 private String address;
15 //getter/setter
16
17 public Integer getId() {
18 return id;
19 }
20
21 public void setId(Integer id) {
22 this.id = id;
23 }
24
25 public String getUsername() {
26 return username;
27 }
28
29 public void setUsername(String username) {
30 this.username = username;
31 }
32
33 public String getAddress() {
34 return address;
35 }
36
37 public void setAddress(String address) {
38 this.address = address;
39 }
40 }

代码如下:
1 package com.example.dell.web;
2
3 import io.swagger.annotations.ApiModelProperty;
4
5 public class RespBean {
6 @ApiModelProperty(value = "用户id")
7 private Integer id;
8 @ApiModelProperty(value = "用户名")
9 private String username;
10 @ApiModelProperty(value = "用户地址")
11 private String address;
12 //getter/setter
13
14 public Integer getId() {
15 return id;
16 }
17
18 public void setId(Integer id) {
19 this.id = id;
20 }
21
22 public String getUsername() {
23 return username;
24 }
25
26 public void setUsername(String username) {
27 this.username = username;
28 }
29
30 public String getAddress() {
31 return address;
32 }
33
34 public void setAddress(String address) {
35 this.address = address;
36 }
37 }
然后启动,运行


此时启动项目,输入http://localhost:8080/swagger-ui.html,能够看到如下页面,说明已经配置成功了:

IDEA+SpringBoot整合Swagger2创建API文档的更多相关文章
- Spring Boot 整合Swagger2构建API文档
1.pom.xml中引入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...
- SpringBoot使用Swagger2构建API文档
后端开发中经常需要对移动客户端提供RESTful API接口,在后期版本快速迭代的过程中,修改接口实现的时候都必须同步修改接口文档,而文档与代码又处于两个不同的媒介,除非有严格的管理机制,不然很容易导 ...
- SpringBoot整合Swagger2搭建API在线文档
Swagger,中文"拽"的意思,它是一个功能强大的在线API在线文档,目前它的版本为2.x,所以称为Swagger2.Swagger2提供了在线文档的查阅和测试功能.利用Swag ...
- 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文档 ...
- Spring Boot中使用Swagger2构建API文档
程序员都很希望别人能写技术文档,自己却很不愿意写文档.因为接口数量繁多,并且充满业务细节,写文档需要花大量的时间去处理格式排版,代码修改后还需要同步修改文档,经常因为项目时间紧等原因导致文档滞后于代码 ...
- 白话SpringCloud | 第十一章:路由网关(Zuul):利用swagger2聚合API文档
前言 通过之前的两篇文章,可以简单的搭建一个路由网关了.而我们知道,现在都奉行前后端分离开发,前后端开发的沟通成本就增加了,所以一般上我们都是通过swagger进行api文档生成的.现在由于使用了统一 ...
- 使用springfox+swagger2书写API文档(十八)
使用springfox+swagger2书写API文档 springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfo ...
- springboot利用swagger构建api文档
前言 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容,见参考资料. S ...
- SpringBoot中使用springfox+swagger2书写API文档
随着前后端的分离,借口文档变的尤其重要,springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfox生成的API文 ...
随机推荐
- pyqt5离线安装教程
目前总结的安装pyqt5,需要的离线安装包,除了每一个包要跟系统版本适配之外,还要考虑包跟包之间的适配.pyqt5跟它开头的一些包要保持是同一个版本,至少有2个小数点的位数是一样的才行,qt5跟它开头 ...
- [iOS]获取地理位置信息
1.在工程的 info.plist 文件中增加两个key( 右键 - Add Row ) Privacy - Location Always and When In Use Usage Descrip ...
- kafka配置内外网同时访问
#内网监听名称,这个在配置文件中没有需要添加 inter.broker.listener.name=INTERNAL #内网监听规则,第一个是内网,第二个是外网,注意端口不一样,端口可以自己定义 li ...
- vs code 提交代码弹框提示:请确保已在git中配置您的“user.name”和“user.email” ——解决方法
修改完项目代码,准备提交到git上,结果提交失败,弹框提示:请确保已在Git中配置您的"user.name"和"user.email" 打开终端,配置运行一下命 ...
- mysql索引相关知识
一.什么情况下会使索引失效? 二.sql优化您们是怎么做的? 1.首先开启数据库慢查询日志,定位到查询效率比较低的sql , 找出对应的sql语句并进行分析 1.表设计是否规范,是否符合三范式的标准( ...
- 修改、编辑pdf
Python 操作 PDF 会用到两个库,分别是:PyPDF2 和 pdfplumber 其中 PyPDF2 可以更好的读取.写入.分割.合并PDF文件,而 pdfplumber 可以更好的读取 PD ...
- js——带暂停、启动功能的定时
简单的封装,将 interval 二次封装,对外提供暂停.启动功能. 不足之处:interval定时间隔是固定的,在调用异步函数的时候,可能会出现bug.例如:在调用ajax异步请求过程中,发送a.b ...
- SQL逻辑查询语句执行顺序—练习题
在做练习题之前要重点熟悉select 的执行顺序 1.SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table& ...
- springboot 集成poi导出word(一)
使用ruoyi-前后端分离版本,根据word模板导出,包含表格和图片. 一.创建模板 列表使用{{}},文本使用[] 二.引入依赖 <!-- excel工具 --> <depende ...
- (转载)史上最详细的docker学习手册
原文链接:https://my.oschina.net/u/1388595/blog/5078146 一.docker入门 1.docker的安装及入门示例 环境准备:docker需要安装在cento ...