Spring Boot 中使用 Swagger
前后端分离开发,后端需要编写接⼝说明⽂档,会耗费⽐较多的时间。
swagger 是⼀个⽤于⽣成服务器接⼝的规范性⽂档,并且能够对接⼝进⾏测试的⼯具。
作用
- ⽣成接⼝说明⽂档
- 对接⼝进⾏测试
使用步骤
添加依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
写配置类
SwaggerConfig/**
* SwaggerConfig 接口文档配置类
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig { /**
* 配置接口文档生成规则
*/
@Bean
public Docket getDocket() {
// 设置文档生成规则
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) // 设置文档信息
.select()
// 设置哪个包下的类需要生成文档
.apis(RequestHandlerSelectors.basePackage("com.luis.fmmall.controller"))
.paths(PathSelectors.any()) // 定义哪些路径的接口需要生成文档
.build(); } /**
* 设置文档信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xxx接口文档")
.description("这里是相关描述")
.version("1.0")
.contact(new Contact("luis",
"https://www.cnblogs.com/luisblog",
"xxx@qq.com"))
.build();
}
}
在控制器类上使用 Swagger 的注解进行相关说明
示例如下:
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理", value = "提供用户的登陆、注册、修改等功能") //类说明
public class UserController { @Resource
private UserService userService; @GetMapping("/login")
@ApiOperation(value = "登陆验证", notes = "用户登陆检查") //方法名说明
@ApiImplicitParams({ //参数说明
@ApiImplicitParam(dataType = "string", name = "username", value = "用户名", required = true),
@ApiImplicitParam(dataType = "string", name = "password", value = "用户密码", required = false, defaultValue = "123")
})
public ResultVo login(@RequestParam("username") String name,
@RequestParam(value = "password", defaultValue = "123") String pwd) {
return userService.checkLogin(name, pwd);
}
}
启动 SpringBoot 应用,访问
http://localhost:8080/swagger-ui.html效果如下:

常用注解说明
@Api:类注解,使用在控制器类上,对类进行说明控制器类 UserController 示例:
@Api(tags = "用户管理", value = "提供用户的登陆、注册、修改等功能") //类说明
public class UserController {
}
@ApiOperation:方法注解,使用在方法上,对方法名进行说明@ApiImplicitParam和@ApiImplicitParams:方法注解,使用在方法上,对方法的形参进行说明单个形参使用
@ApiImplicitParam,多个形参使用@ApiImplicitParams控制器类 UserController 的 login 方法示例:
@GetMapping("/login")
@ApiOperation(value = "登陆验证", notes = "用户登陆检查") //方法名说明
@ApiImplicitParams({ //参数说明
@ApiImplicitParam(dataType = "string", name = "username", value = "用户名", required = true),
@ApiImplicitParam(dataType = "string", name = "password", value = "用户密码", required = false, defaultValue = "123")
})
public ResultVo login(@RequestParam("username") String name,
@RequestParam(value = "password", defaultValue = "123") String pwd) {
return userService.checkLogin(name, pwd);
}
@ApiModel和@ApiModelProperty:当接⼝的形参或返回值为对象类型时,在实体类中添加此注解说明接口的返回值为 ResultVo 对象示例:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "ResultVo 对象", description = "返回给前端的封装数据") //返回的类说明
public class ResultVo { // 响应给前端的状态码
@ApiModelProperty("响应状态码") //属性说明
private int code; // 响应给前端的提示信息
@ApiModelProperty("提示信息") //属性说明
private String msg; // 响应给前端的数据
@ApiModelProperty("数据") //属性说明
private Object data;
}
接口的形参为 User 实体对象示例:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "User 对象",description = "⽤户/买家信息")
public class User {
@ApiModelProperty(dataType = "int",required = false)
private int userId;
@ApiModelProperty(dataType = "String",required = true, value = "⽤
户注册账号")
private String userName;
@ApiModelProperty(dataType = "String",required = true, value = "⽤
户注册密码")
private String userPwd;
@ApiModelProperty(dataType = "String",required = true, value = "⽤
户真实姓名")
private String userRealname;
@ApiModelProperty(dataType = "String",required = true, value = "⽤
户头像url")
private String userImg;
}
@ApiIgnore:接⼝⽅法注解,添加此注解的⽅法将不会⽣成到接⼝⽂档中
swagger-ui 插件
发现一个规律,越学到最后,越是有惊喜,有不有?
swagger-ui 插件是一款 UI 美化插件,是基于 swagger 的。
之前使用的默认 swagger 文档和调试页面如果使用起来不太顺畅,可以试试这款 swagger-ui 插件。
使用
添加依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
重启 SpringBoot 应用,访问
http://localhost:8080/doc.html效果如下:



还等什么,赶紧装插件去~
Spring Boot 中使用 Swagger的更多相关文章
- Spring Boot中使用Swagger CodeGen生成REST client
文章目录 什么是Open API规范定义文件呢? 生成Rest Client 在Spring Boot中使用 API Client 配置 使用Maven plugin 在线生成API Spring B ...
- spring boot 中使用swagger 来自动生成接口文档
1.依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...
- spring boot 中使用swagger
一.pom.xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox ...
- 【swagger】1.swagger提供开发者文档--简单集成到spring boot中【spring mvc】【spring boot】
swagger提供开发者文档 ======================================================== 作用:想使用swagger的同学,一定是想用它来做前后台 ...
- Spring Boot中使用Swagger2构建强大的RESTful API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
- Spring Boot中使用Swagger2构建API文档
程序员都很希望别人能写技术文档,自己却很不愿意写文档.因为接口数量繁多,并且充满业务细节,写文档需要花大量的时间去处理格式排版,代码修改后还需要同步修改文档,经常因为项目时间紧等原因导致文档滞后于代码 ...
- 在Spring Boot中使用swagger-bootstrap-ui
在Spring Boot中使用swagger-bootstrap-ui swagger-bootstrap-ui是基于swagger接口api实现的一套UI,因swagger原生ui是上下结构的,在浏 ...
- Spring Boot中使用Swagger2构建RESTful APIs
关于 Swagger Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. S ...
- Spring Boot中使用Swagger2自动构建API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
随机推荐
- Excelize 2.5.0 正式发布,这些新增功能值得关注
Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准.可以使用它来读取.写入由 Microsoft Exc ...
- Mybatis-Plus高级之LambdaQueryWrapper,Wrappers.<实体类>lambdaQuery的使用
一.前言 小编今天又来分享干货了,绝对的干净又卫生,大伙请放心食用哈!Mybatis-Plus我们经常使用,但是里面的很多功能,小编开始只是知道一点点,做个增删改查没问题.小编在新项目中发现,大神们不 ...
- Spire.Cloud 私有化部署教程(三) - Windows 系统
本教程主要介绍如何在 Windows 系统上实现 Spire.Cloud 私有化部署. 详细步骤如下: 一.安装依赖 我们的私有部署的依赖有 Nodejs.MySQL.Redis 和 RabbitMQ ...
- 04_Django-模板变量/标签/过滤器/继承-url反向解析
04_Django-模板变量/标签/过滤器/继承-url反向解析 视频:https://www.bilibili.com/video/BV1vK4y1o7jH 博客:https://blog.csdn ...
- KingbaseES 创建只读(read_only)用户
数据库版本: prod=> select version(); version --------------------------------------------------------- ...
- Ros入门21讲
一.ROS是什么? ROS=通信机制+开发工具+应用功能+生态系统 目的:提高机器人研发中的软件复用率. 1.ROS中的通信机制 松耦合分布式通信: 注意:什么是耦合.紧耦合.松耦合? 1.1 耦合 ...
- DFS算法-求集合的所有子集
目录 1. 题目来源 2. 普通方法 1. 思路 2. 代码 3. 运行结果 3. DFS算法 1. 概念 2. 解题思路 3. 代码 4. 运行结果 4. 对比 1. 题目来源 牛客网,集合的所有子 ...
- C#,启动exe程序并传参(参数间带&符号)方法
入参格式例如:C:\\Users\\Administrator\\Desktop\\测试\\测试\\bin\\Debug\\测试.exe type=1^&card_no=123 public ...
- 讲讲 tcp_tw_recycle,tcp_tw_reuse
文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247485332&idx=1&sn=59823ce1 ...
- 7.nexus版本升级
nexus-3.14.0升级到3.15.2 首先来看下原来的服务目录: nexus-3.14.0-04 sonatype-work 注意:nexus-3.14.0-04是应用程序包,sonatype- ...