学习目标:
- 了解swagger的作用和概念
- 了解前后端分离
- 在springboot中集成swagger
主流的解决方案:Vue+spring boot
前后端分离时代:
- 后端:后端控制层、服务层、数据访问层
- 前端:前端控制层、视图层
- 前后端如何交互:->通过API来来访问
- 前后端相互独立,耦合度低,前后端也可以部署在不同的服务器上
产生的问题:
- 前后端集成联调苦难,前端后端人员无法“及时沟通,尽早解决”,会导致问题集中爆发
解决方案:
- 首先指定schema【计划的提纲】,实时更新最新API,降低集成风险
- 前后端分离需要提供一个实时的接口工具(早期测试接口postman)
- 号称世界上最流行的API框架
- RestFul API 文档在线自动生成工具->API文档和API的定义同步更新
- 直接运行,可以在线测试API接口
- 支持多种语言(Java、PHP等)
官网:https://swagger.io/
在项目中使用swagger需要导入Java包
- swagger2
- ui
1、新建springboot项目
2、导入相关依赖
io.springfox springfox-swagger22.9.2 io.springfox springfox-swagger-ui2.9.2
3、编写helloworld进行测试
package edu.kuquan.swagger.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
// /error 默认的请求
@RequestMapping("/hello")
public String hello() {
return "Hello world!";
}
}
4、配置swagger(编写配置类)
package edu.kuquan.swagger.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {
}
springboot2.6以上需要额外配置properties
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
5、访问swagger
链接:http://localhost:8080/swagger-ui.html
根据填写的映射方式,显示对应的请求方式
springboot配置swagger swagger信息模块配置package edu.kuquan.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 // 开启swagger2
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
// swagger配置信息
private ApiInfo apiInfo() {
return new ApiInfo("swagger-demo 工程 Api Documentation", "穷且益坚,不坠青云之志", "1.0", "urn:tos", new Contact("苦泉", "", ""), "Apache 2.0", "https://space.bilibili.com/24354849", new ArrayList());
}
}
swagger配置扫描接口
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("edu.kuquan.swagger.config")) // 指定要扫描的包
.build();
}
配置扫描的路径
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("edu.kuquan.swagger.config")) // 指定要扫描的包
.paths(PathSelectors.ant("/*")) // 确定要扫描的路径
.build();
}
配置是否启动swagger
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true); // true:启动swagger false:关闭swagger
}
如何确定swagger的使用环境,只在生产和测试环境使用,发布的时候不适用
- 判断是不是生产环境
- 注入enable
@Bean
public Docket docket(Environment environment) {
// 获取项目环境,设置swagger的显示环境
Profiles profiles = Profiles.of("dev", "test");
// 通过profiles判断当前所在环境
boolean b = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(b);
}
配置swagger的API文档分组
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("ku");
}
如何配置多个分组?
配置多个Docket即可
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("ku");
}
@Bean
public Docket docket2() {
return new Docket(DocumentationType.SWAGGER_2).groupName("test");
}
在swagger中显示对象的结构
// 只要我们的接口中返回值中存在实体类,就会被swagger给扫描到
@PostMapping("/user")
public User getuser(String name, Integer age) {
return new User(name, age);
}
结果:
给对象属性添加注解
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("姓名")
public String name;
@ApiModelProperty("年龄")
public Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public User() {}
}
结果:
给接口增加注解
@ApiOperation("你好,世界!")
@GetMapping("/hello")
public String hello() {
return "Hello world!";
}
结果:
swagger接口测试
无参数测试
直接执行便可
有参数测试
赋值后执行即可



