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. 项目地址

  spring-boot-swagger2-demo

SpringBoot整合Swagger2详细教程的更多相关文章

  1. SpringBoot整合Swagger2

    相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档. 手写Api文档的几个痛点: 文档需 ...

  2. springboot 整合Swagger2的使用

    Swagger2相较于传统Api文档的优点 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工 ...

  3. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

  4. SpringBoot整合Swagger2及使用

    简介 swagger是一个流行的API开发框架,这个框架以"开放API声明"(OpenAPI Specification,OAS)为基础, 对整个API的开发周期都提供了相应的解决 ...

  5. Struts2+Spring4+Hibernate4整合超详细教程

    Struts2.Spring4.Hibernate4整合 超详细教程 Struts2.Spring4.Hibernate4整合实例-下载 项目目的: 整合使用最新版本的三大框架(即Struts2.Sp ...

  6. SpringBoot(七):SpringBoot整合Swagger2

    原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...

  7. SpringBoot整合Swagger2(Demo示例)

    写在前面 由于公司项目采用前后端分离,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理 ...

  8. Springboot 整合 MyBatisPlus[详细过程]

    Springboot 整合 MyBatisPlus[详细过程] 提要 这里已经将Springboot环境创建好 这里只是整合MyBatis过程 引入Maven依赖 添加MyBatisPlus启动依赖, ...

  9. Spring Data JPA系列2:SpringBoot集成JPA详细教程,快速在项目中熟练使用JPA

    大家好,又见面了. 这是Spring Data JPA系列的第2篇,在上一篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring Data JPA,傻傻分不清楚?给你个 ...

随机推荐

  1. centos搭建dns服务

    原文:(https://www.myjinji.top/articles/2020/04/02/1585800289945.html)[https://www.myjinji.top/articles ...

  2. 一次看完28个关于ES的性能调优技巧,很赞,值得收藏!

    因为总是看到很多同学在说Elasticsearch性能不够好.集群不够稳定,询问关于Elasticsearch的调优,但是每次都是一个个点的单独讲,很多时候都是case by case的解答,本文简单 ...

  3. 面试阿里,字节跳动90%会被问到的Java异常面试题集,史上最全系列!

    Java异常架构与异常关键字 Java异常简介 Java异常是Java提供的一种识别及响应错误的一致性机制. Java异常机制可以使程序中异常处理代码和正常业务代码分离,保证程序代码更加优雅,并提高程 ...

  4. UnitTest_墨振文档

    目录 一.框架介绍 1 二.四大组件 2 三.ddt数据驱动 3 一.框架介绍 unittest框架是python 自带的一个作为单元测试的测试框架,在最初叫pyUnit,相当与Java语言中的Jun ...

  5. swupdate实例

    平台:imx8mm 系统:linux 4.4   如果需要系统了解swupdate,请参考文章:嵌入式系统更新swupdate分类   一.制作升级包 emmcsetup.lua用来描述update执 ...

  6. 如何卸载MathType 7?

    作为好用的公式编辑器,一般情况下是不会将其从电脑上卸载的,但是当电脑负荷过多,导致电脑运行缓慢时,就需要考虑卸载一些软件,本节就来学习卸载MathType 7的方法. 具体操作步骤如下: 1.打开控制 ...

  7. JUC并发工具包之CountDownLatch

    1.介绍 本文将介绍CountDownLatch并给出实践中的几个例子,通过使用CountDownLatch我们可以让一个线程阻塞直到其他一个或多个线程执行完成. A synchronization ...

  8. Dapr DotNet5 HTTP 调用

    Dapr DotNet5 HTTP 调用 版本介绍 Dotnet 版本:5.0.100 Dapr dotnet 版本:0.12.0-preview01 注意: Asp.Net Core 项目中的 la ...

  9. 区块链V1版本实现之三

    部分程序代码(区块链的定义及遍历打印): 1 //创建区块链,使用Block数组模拟 2 type BlockChain struct { 3 Blocks []*Block 4 } 5 6 //实现 ...

  10. Kubernetes K8S之固定节点nodeName和nodeSelector调度详解

    Kubernetes K8S之固定节点nodeName和nodeSelector调度详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-mas ...