SpringBoot整合Swagger2详细教程
1. 简介
随着前后端分离开发模式越来越流行,编写接口文档变成了开发人员非常头疼的事。而Swagger是一个规范且完整的web框架,用于生成、描述、调用可视化的RESTful风格的在线接口文档,并解决手写文档时编写和更新以及测试的复杂问题。
2. 示例代码
- 创建项目
- 修改pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.c3stones</groupId>
<artifactId>spring-boot-swagger2-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-swagger2-demo</name>
<description>Spring Boot Swagger2 Demo</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.8.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- 创建Swagger2配置类
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;
/**
* Swagger2配置类
*
* @author CL
*
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
/**
* 注入Docket
*
* @return
*/
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(setApiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.c3stones.controller")).paths(PathSelectors.any())
.build();
}
/**
* 配置在线文档的基本信息
*
* @return
*/
private ApiInfo setApiInfo() {
return new ApiInfoBuilder().title("SpringBoot整合Swagger2示例")
.description("<a href='https://www.cnblogs.com/cao-lei/' target='_blank'>欢迎访问我的博客</a>")
.termsOfServiceUrl("https://www.cnblogs.com/cao-lei/").version("V1.0").build();
}
}
- 创建实体
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* 用户信息
*
* @author CL
*
*/
@Data
@AllArgsConstructor
public class User {
/**
* 用户ID
*/
private Long id;
/**
* 用户名称
*/
private String username;
/**
* 年龄
*/
private Integer age;
}
- 创建Controller
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.c3stones.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
/**
* 用户Controller
*
* @author CL
*
*/
@Api(tags = "User / 用户信息")
@RestController
@RequestMapping(value = "/user")
public class UserController {
/**
* 获取用户信息
*
* @param user 用户信息
* @return
*/
@RequestMapping(value = "/get", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "获取用户信息")
@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, type = "Long"),
@ApiImplicitParam(name = "username", value = "用户名称", type = "String"),
@ApiImplicitParam(name = "age", value = "年龄", type = "Integer") })
public User get(User user) {
return new User(1L, "C3Stones", 23);
}
/**
* 获取用户列表
*
* @param user 用户信息
* @return
*/
@SuppressWarnings("serial")
@RequestMapping(value = "/listData", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
@ApiOperation(value = "获取用户列表")
@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", type = "Long"),
@ApiImplicitParam(name = "username", value = "用户名称", type = "String"),
@ApiImplicitParam(name = "age", value = "年龄", type = "Integer") })
public List<User> listData(User user, @ApiIgnore HttpRequest request) {
return new ArrayList<User>() {
{
add(new User(1L, "张三", 23));
add(new User(2L, "李四", 24));
add(new User(3L, "王五", 25));
}
};
}
}
- 创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类
*
* @author CL
*
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3. 测试
- 启动项目
- 浏览器访问
http://127.0.0.1:8080/swagger-ui.html
4. 项目地址
SpringBoot整合Swagger2详细教程的更多相关文章
- SpringBoot整合Swagger2
相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档. 手写Api文档的几个痛点: 文档需 ...
- springboot 整合Swagger2的使用
Swagger2相较于传统Api文档的优点 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工 ...
- SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法
原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...
- SpringBoot整合Swagger2及使用
简介 swagger是一个流行的API开发框架,这个框架以"开放API声明"(OpenAPI Specification,OAS)为基础, 对整个API的开发周期都提供了相应的解决 ...
- Struts2+Spring4+Hibernate4整合超详细教程
Struts2.Spring4.Hibernate4整合 超详细教程 Struts2.Spring4.Hibernate4整合实例-下载 项目目的: 整合使用最新版本的三大框架(即Struts2.Sp ...
- SpringBoot(七):SpringBoot整合Swagger2
原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...
- SpringBoot整合Swagger2(Demo示例)
写在前面 由于公司项目采用前后端分离,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理 ...
- Springboot 整合 MyBatisPlus[详细过程]
Springboot 整合 MyBatisPlus[详细过程] 提要 这里已经将Springboot环境创建好 这里只是整合MyBatis过程 引入Maven依赖 添加MyBatisPlus启动依赖, ...
- Spring Data JPA系列2:SpringBoot集成JPA详细教程,快速在项目中熟练使用JPA
大家好,又见面了. 这是Spring Data JPA系列的第2篇,在上一篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring Data JPA,傻傻分不清楚?给你个 ...
随机推荐
- php获取字符串长度
function len($zfc){ $arr = []; $len = mb_strlen($zfc); for ($i = 0; $i < $len; $i++) { array_push ...
- 基于chaosblade的故障注入平台小试
当今社会互联网应用越来越广泛,用户量日益剧增.在人们对互联网服务的依赖性增大的同时,也对服务的可用性和体验感有了更高的要求.那么如何保障服务在运营过程中能一直给用户提供稳定的.不间断的.可靠可信的服务 ...
- u盘插电脑没反应的三大原因,以及解决方法
相信大家在使用U盘的过程中免不了会遇到这样的情况:u盘虽然与电脑连接,但是插上后却没有反应.很多小伙伴都摸不着头脑不知道到底是哪里出了错.其实大家也不用过于心急,只要找到了原因便可很快得到解决. u盘 ...
- 一万三千字的HashMap面试必问知识点详解
目录 概论 Hasmap 的继承关系 hashmap 的原理 解决Hash冲突的方法 开放定址法 再哈希法 链地址法 建立公共溢出区 hashmap 最终的形态 Hashmap 的返回值 HashMa ...
- 【mq读书笔记】mq索引文件刷盘
索引文件的刷盘并不是采取定时刷盘机制,而是每更新一次索引文件就会将上一次的改动刷写到磁盘. 同步刷盘: GroupCommitRequest将被提交到GroupCommitService线程,Grou ...
- LaTeX中的参考文献BibTex
设置: BibTex代码及注释: 显示效果:
- CentOS下搭建VNC/TEAMVIEW/SSH无密码登录
VNC 配置桌面 # 安装gnome桌面环境 yum groupinstall Desktop -y # 安装中文语言支持包(可选) yum groupinstall 'Chinese Support ...
- 使用Git,10件你可能需要“反悔”的事
DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...
- Alpha冲刺-第三次冲刺笔记
Alpha冲刺-冲刺笔记 这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2 这个作业要求在哪里 https://edu.cnblogs. ...
- Robot Framework接口自动化案例分享⑦——Jenkins持续集成
一.RobotFramework插件安装 1.Jenkins首页->系统管理->插件管理->可选插件-> 2.搜索robot,点击直接安装 二.任务参数配置 1.新建任务 Je ...