手把手教你SpringBoot快速集成Swagger的配置过程
作者:常安、 时间:2023-10-30 01:16:22
导语
相信大家无论是做前端还是做后端的,都被接口接口文档所折磨过,前端抱怨接口文档和后端给的不一致,后端抱怨写接口文档很麻烦,所以Swagger就诞生了。直接配置即可自动生成接口文档,而且提供了高效的API测试
话不多说直接开干
导入SpringBoot集成Swagger所需要的依赖
<!--web方便测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- swagger2核心包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger-ui 可视化界面 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swagger可视化界面可分为三个区域
Swagger相关配置
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 //开启Swagger的使用
public class SwaggerConfig {
@Bean //Swagger的使用主要是要将docket对象传入IOC容器
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) //关于文档的各种信息
.enable(true) //使Swagger生效
.groupName("常安祖")
.select()//选择扫描的接口
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))//指定扫描的接口
.build();
}
public ApiInfo apiInfo(){
Contact contact = new Contact("长安","https://blog.csdn.net/weixin_45647685","719801748@qq.com");//个人的联系方式
return new ApiInfo("长安的文档", "长安的开发文档", "1.0", "urn:tos",null, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());//文档的各种信息
}
}
@ApiModel( ) //主要用来标注返回的实体类
@ApiModelProperty( ) //主要用来标注实体类中的属性
案例:
@ApiModel("用户的实体类")
public class User implements Serializable {
@ApiModelProperty("用户的id")
private Integer id;
@ApiModelProperty("用户的姓名")
private String name;
@ApiModelProperty("用户的年纪")
private Integer age;
public Integer getId() {
return id;
}
public User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@ApiModelProperty用来标注API接口
案例:
package com.yangzihao.controller;
import com.yangzihao.entity.User;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class UserController {
@ApiModelProperty("得到一个User")
@GetMapping("/getUser")
public User getUser(){
return new User(1,"测试",18);
}
}
进入Swagger可视化界面
使用Swagger进行接口测试
执行
来源:https://blog.csdn.net/weixin_45647685/article/details/113850616
标签:SpringBoot,集成,Swagger
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决Javaweb 提交表单到servlet时出现空白页面,但网站不报错问题
2021-06-21 20:26:48
![](https://img.aspxhome.com/file/2023/9/83589_0s.png)
关于Android中drawable必知的一些规则
2021-08-26 16:24:41
Java annotation元注解原理实例解析
2022-09-08 00:42:45
使用mybatis插件PageHelper实现分页效果
2023-03-29 15:12:03
![](https://img.aspxhome.com/file/2023/1/65271_0s.jpg)
spring mvc中的@PathVariable获得请求url中的动态参数
2023-08-22 22:08:40
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2022-02-28 02:43:26
![](https://img.aspxhome.com/file/2023/2/85542_0s.jpg)
android实现获取正在运行的应用程序
2022-10-14 09:23:38
![](https://img.aspxhome.com/file/2023/4/139594_0s.png)
Android获取设备传感器的方法
2022-10-07 10:50:16
![](https://img.aspxhome.com/file/2023/9/137219_0s.jpg)
C语言实现简单版三子棋
2023-03-01 22:36:30
Java多线程之读写锁分离设计模式
2021-06-08 07:20:48
实例讲解Java中动态代理和反射机制
2022-06-29 04:00:50
MyBatis动态SQL特性详解
2022-10-03 03:21:50
![](https://img.aspxhome.com/file/2023/4/77974_0s.png)
Java使用线程同步解决线程安全问题详解
2022-02-28 02:03:24
![](https://img.aspxhome.com/file/2023/9/78029_0s.png)
Android编程滑动效果之Gallery仿图像集浏览实现方法
2022-11-19 15:21:58
![](https://img.aspxhome.com/file/2023/7/139017_0s.png)
java 多线程的三种构建方法
2023-07-01 22:18:26
java中ZXing 生成、解析二维码图片的小示例
2022-07-24 11:50:39
![](https://img.aspxhome.com/file/2023/3/125633_0s.png)
SimpleCommand实现图片下载(二)
2023-05-21 00:25:17
Java线程池大小的设置方法实例
2022-10-04 04:20:19
java开发分布式服务框架Dubbo调用过程
2022-04-13 14:29:55
![](https://img.aspxhome.com/file/2023/2/71882_0s.png)
java compare compareTo方法区别详解
2022-06-26 08:13:55