SpringBoot整合Swagger2实现接口文档
展示一下
访问方式一
访问地址:http://localhost:8080/swagger-ui.html#/
首页

详情页

访问方式二
访问地址:http://localhost:8080/doc.html
首页

侧边栏

详情页


用途介绍
现在的开发模式都是前后端分离,在联调阶段,后端同学需要向前端同学提供api文档。
一个清晰加美观的文档可以减少很多口水仗,是维护和谐开发氛围的必需品。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
它有如下几个特点:
1.及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)
2.规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)
3.一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)
4.可测性 (直接在接口文档上进行测试,以方便理解业务)
尝试一下
1、配置文件
SpringBoot项目pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-swagger</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这里需要注意一个版本对应的问题,如果使用了高版本的SpringBoot框架,低版本的Swagger,
会出现如下报错:
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
这是因为:因为Springfox 使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。
以下几个版本是兼容的
| SpringBoot版本 | Swagger版本 |
|---|---|
| 2.5.6 | 2.9.2 |
| SpringBoot版本 | Swagger版本 |
|---|---|
| 2.6.5 | 3.0.0 |
2、项目代码
项目结构

SwaggerConfig.java
package com.example.springbootswagger.config;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Bean
public Docket createDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true)
.groupName("我的接口文档")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springbootswagger.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//标题
.title("凤求凰")
//作者、链接、邮箱等
.contact(new Contact(
"司马相如",
"https://hanyu.baidu.com/shici/detail?pid=ecb82707a98c418995c5a0c50b770af0&from=kg0",
""
))
//描述
.description("有一美人兮,见之不忘。\n" +
"一日不见兮,思之如狂。\n" +
"凤飞翱翔兮,四海求凰。\n" +
"无奈佳人兮,不在东墙。\n" +
"将琴代语兮,聊写衷肠。\n" +
"何日见许兮,慰我彷徨。\n" +
"愿言配德兮,携手相将。\n" +
"不得於飞兮,使我沦亡。\n" +
"凤兮凤兮归故乡,遨游四海求其凰。\n" +
"时未遇兮无所将,何悟今兮升斯堂!\n" +
"有艳淑女在闺房,室迩人遐毒我肠。\n" +
"何缘交颈为鸳鸯,胡颉颃兮共翱翔!\n" +
"凰兮凰兮从我栖,得托孳尾永为妃。\n" +
"交情通意心和谐,中夜相从知者谁?\n" +
"双翼俱起翻高飞,无感我思使余悲。")
//更新说明
.termsOfServiceUrl("这是第一版")
//版本号
.version("1.0.0").build();
}
}
TestController.java
package com.example.springbootswagger.controller;
import com.example.springbootswagger.req.AddReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
@RestController
@Api(tags = {"测试接口类"}, hidden = true)
@RequestMapping("/test")
public class TestController {
@ApiOperation("GET请求,查询方法")
@GetMapping("/query")
public String query() {
return "查询成功";
}
@ApiImplicitParams({
@ApiImplicitParam(name = "param1", value = "参数1", required = true),
@ApiImplicitParam(name = "param2", value = "参数2", required = false)
})
@ApiOperation("PUT请求,添加方法")
@PutMapping("/update")
public String update(
@RequestParam(required = true) String param1,
@RequestParam(required = false) String param2) {
return "更新成功";
}
@ApiOperation("POST请求,修改方法")
@PostMapping("/add")
public String add(@RequestBody AddReq addReq) {
return "添加成功";
}
@ApiImplicitParam(name = "id", value = "用户ID", required = true)
@ApiOperation("DELETE请求,删除方法")
@DeleteMapping("/del")
public String del(Long id) {
return "删除成功";
}
}
AddReq.java
package com.example.springbootswagger.req;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("添加参数")
public class AddReq {
@ApiModelProperty("名字")
private String name;
@ApiModelProperty("密码")
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
SpringbootSwaggerApplication.java
package com.example.springbootswagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@SpringBootApplication
public class SpringbootSwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSwaggerApplication.class, args);
}
}
SpringBoot整合Swagger2实现接口文档的更多相关文章
- Springboot集成swagger2生成接口文档
[转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11509884.html 作者:jstarseven 码字挺辛苦的..... 一 ...
- SpringBoot整合Swagger3生成接口文档
前后端分离的项目,接口文档的存在十分重要.与手动编写接口文档不同,swagger是一个自动生成接口文档的工具,在需求不断变更的环境下,手动编写文档的效率实在太低.与swagger2相比新版的swagg ...
- IDEA+SpringBoot整合Swagger2创建API文档
------------恢复内容开始------------ 1.创建SpringBoot项目 2.选择快捷方式创建springboot项目 3.工程文件树形图 4.pom.xml中导入Swagger ...
- swagger2的接口文档
以前见过一个swagger2的接口文档,特别好用,好看,对接口中入参描述的很详细:适合用于项目的开发 后来自己做项目的时候,没有找到这个swagger版本 <dependency> < ...
- Springboot+swagger2的接口文档开发
一.创建一个SpringBoot项目 1. 2. 3. 4. 把web里的web选中,SQL里选择自己需要的,点击next 二.创建各项所需的controller,configure等 1. 项目布局 ...
- spring-boot-route(五)整合Swagger生成接口文档
目前,大多数公司都采用了前后端分离的开发模式,为了解决前后端人员的沟通问题,后端人员在开发接口的时候会选择使用swagger2来生成对应的接口文档,swagger2提供了强大的页面调试功能,这样可以有 ...
- SpringBoot使用Swagger2构建API文档
后端开发中经常需要对移动客户端提供RESTful API接口,在后期版本快速迭代的过程中,修改接口实现的时候都必须同步修改接口文档,而文档与代码又处于两个不同的媒介,除非有严格的管理机制,不然很容易导 ...
- spring-boot-route(六)整合JApiDocs生成接口文档
上一篇文章中介绍了使用Swagger生成接口文档,非常方便,功能也十分强大.如果非要说Swaager有什么缺点,想必就是注解写起来比较麻烦.如果我说有一款不用写注解,就可以生成文档的工具,你心动了吗? ...
- Spring Boot 整合Swagger2构建API文档
1.pom.xml中引入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...
- SpringFox swagger2 and SpringFox swagger2 UI 接口文档生成与查看
依赖: <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency ...
随机推荐
- [转帖]jmeter实现不写代码把测试结果存入execl
这里使用数据库作为中间件来实现不写代码就把测试结果存入execl,下面是步骤 1.新建一个setup线程组用来设置数据库连接信息和新建数据库,如下图所示,我们使用sqlite数据库来存储信息,因为不需 ...
- [转帖]unmatched(riscv64)上编译,安装和移植SPEC CPU 2006
https://zhuanlan.zhihu.com/p/429399630 Linux ubuntu 5.11.0-1021-generic #22-Ubuntu SMP Tue Sep 28 15 ...
- 【转帖】dl.google.com的国内镜像源
dl.google.com不能访问 1.通过https://ping.chinaz.com/解析出dl.google.com的国内代理 2.C:\Windows\System32\drivers\et ...
- 【转帖】x86服务器中网络性能分析与调优(高并发、大流量网卡调优)
最近在百度云做一些RTC大客户的项目,晚上边缘计算的一台宿主机由于CPU单核耗被打满,最后查到原因是网卡调优没有生效,今天查了一下网卡调优的资料,欢迎大家共同探讨. 一.网卡调优方法 1.Broadc ...
- [转帖]12.JVM运行时数据区之虚拟机栈概述
https://blog.csdn.net/u011069294/article/details/107050001 目录 1. 内存中的栈与堆 2.栈的优点 1. 内存中的栈与堆 栈是运行时单位,堆 ...
- killall 以及 pkill 等命令
https://zhidao.baidu.com/question/1500084252693125099.html // 通过 killall 命令killall nginx// 通过 pkill ...
- 自建邮箱服务器 EwoMail 发送邮件的办法
总结来源: http://doc.ewomail.com/docs/ewomail/changguipeizhi 1. 首先这个机器不能安装dovecot等软件,不然安装脚本会失败. 2. 下载安装文 ...
- 30岁程序媛求职路复盘:文转码+失业半年+PHP如何涨薪5K!?
这篇文章来自一位群友的分享: 这篇文章写于下班路上,刚刚入职不久,我想再冲刺一下大厂,阳哥建议我坚持总结打卡,可以尝试写写博客. 那我就从这篇开始吧,希望开个好头! 上班的感觉真好 今天是入职的第二周 ...
- vite配置开发环境和生产环境
为什么需要境变量的配置 在很多的时候,我们会遇见这样的问题. 开发环境的接口是:http://test.com/api 但是我们的生产环境地址是:http://yun.com/api 此时,我们打包的 ...
- 从零开始配置vim(28)——代码的编译、运行与调试
在前面几个章节,我们逐渐为 Vim 配置了语法高亮.代码的跳转和自动补全功能.现在的 Vim 已经可以作为代码编辑器来使用了.但是想将它作为日常发开的主力编辑器来用还需要很长一段路要走,其中一个就是要 ...