1. Swagger简介
1.1 前后端分离
Vue + SpringBoot
后端时代:前端只用管理静态页面;html->后端。模板引擎 JSP->后端是主力
前后端分离时代:
- 后端:后端控制层、服务层、数据访问层【后端团队】
- 前端:前端控制层、视图层【前端团队】
- 伪造后端数据,json。已经存在了,不需要后端,前端工程依旧能够跑起来。
前后端如何交互?->API
前后端相对独立,松耦合。
前后端甚至可以部署在不同的服务器上。
产生一个问题:
- 前后端集成联调,前端人员和后端人员无法做到“即使协商,尽早解决”,最终导致问题集中爆发;
解决方案:
- 首先指定schema计划的提纲,实时更新最新API,降低集成的风险;
- 早些年:指定word计划文档;
- 前后端分离:
- 前端测试后端接口:postman
- 后端提供接口,需要实时更新最新的消息及改动。
1.2 Swagger
- 号称世界上最流行的Api框架。
- Restful Api文档在线自动生成工具->Api文档与Api定义同步更新
- 直接运行,可以在线测试Api接口
- 支持多种语言:(Java,Php...)
- 官网:http://swagger.io/
- 在项目使用Swagger需要springbox swagger2 ui;
2.SpringBoot集成Swagger
2.1 新建Springboot web项目
新建后删除冗余的文件,pom有可能报错,查看maven中config路径下settings文件配置,配置成阿里镜像。
2.2 导入依赖
<?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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>swagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>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>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3 新建hello工程
package com.example.swagger.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
2.4 配置Swagger -> Config
package com.example.swagger.config;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
package com.example.swagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
2.5 http://localhost:8080/swagger-ui.html
3. 配置Swagger信息
package com.example.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 java.util.ArrayList;
@Configuration
public class SwaggerConfig {
//配置了Swagger的Docket的bean实例
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
//配置Swagger信息=apiInfo
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("jak", "www.baidu.com", "111@qq.com");
return new ApiInfo("title",
"discription",
"version",
"termOfServiceUrl",
contact,"license",
"licenseUrl",
new ArrayList<>());
}
}
4.Swagger配置扫描接口
package com.example.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import java.util.ArrayList;
@Configuration
public class SwaggerConfig {
//配置了Swagger的Docket的bean实例
//enable是否启动Swagger,如果为False,则Swagger不能在浏览器中访问
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//RequestHandlerSelectors配置要扫描接口的方式
//basePackage:指定要扫描的包
//any():扫描全部
//none():不扫描
//withClassAnnotaion:扫描类上的注解,参数是一个注解的反射对象
//withMethodAnnotation:扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
//paths():过滤什么路径
.paths(PathSelectors.ant("/example/**"))
.build();
}
//配置Swagger信息=apiInfo
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("jak", "www.baidu.com", "111@qq.com");
return new ApiInfo("title",
"discription",
"version",
"termOfServiceUrl",
contact,"license",
"licenseUrl",
new ArrayList<>());
}
}
4.1 配置是否启动Swagger
package com.example.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import java.util.ArrayList;
@Configuration
public class SwaggerConfig {
//配置了Swagger的Docket的bean实例
//enable是否启动Swagger,如果为False,则Swagger不能在浏览器中访问
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(false)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.build();
}
//配置Swagger信息=apiInfo
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("jak", "www.baidu.com", "111@qq.com");
return new ApiInfo("title",
"discription",
"version",
"termOfServiceUrl",
contact,"license",
"licenseUrl",
new ArrayList<>());
}
}
4.2 我只希望我的Swagger在生产环境中使用,在发布的时候不使用
- 判断是不是生产环境 flag = false
- 注入enable(flag)
package com.example.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;
import java.util.ArrayList;
@Configuration
public class SwaggerConfig {
//配置了Swagger的Docket的bean实例
//enable是否启动Swagger,如果为False,则Swagger不能在浏览器中访问
@Bean
public Docket docket(Environment environment) {
//设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev", "test");
//通过environment.acceptsProfiles判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
System.out.println("flag: " + flag);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.build();
}
//配置Swagger信息=apiInfo
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("jak", "www.baidu.com", "111@qq.com");
return new ApiInfo("title",
"discription",
"version",
"termOfServiceUrl",
contact,"license",
"licenseUrl",
new ArrayList<>());
}
}
4.3 配置API文档的分组
.groupName("jak")
如何配置多个分组;多个Docket实例即可
package com.example.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;
import java.util.ArrayList;
@Configuration
public class SwaggerConfig {
@Bean
public Docket docket1() {
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2() {
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket3() {
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
//配置了Swagger的Docket的bean实例
//enable是否启动Swagger,如果为False,则Swagger不能在浏览器中访问
@Bean
public Docket docket(Environment environment) {
//设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev", "test");
//通过environment.acceptsProfiles判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
System.out.println("flag: " + flag);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("jak")
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.build();
}
//配置Swagger信息=apiInfo
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("jak", "www.baidu.com", "111@qq.com");
return new ApiInfo("title",
"discription",
"version",
"termOfServiceUrl",
contact,"license",
"licenseUrl",
new ArrayList<>());
}
}
5 . 实体类配置
package com.example.swagger.pojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
//@Api("注释")
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
public String userName;
@ApiModelProperty("密码")
public String password;
}
package com.example.swagger.controller;
import com.example.swagger.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
//Operation接口
@Api(tags = "Hello控制器")
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
//只要我们的接口中,返回值中存在实体类,他就会被扫描到Swagger中
@PostMapping(value = "/user")
public User user() {
return new User();
}
//Operation,不是放在类上的,是方法
@ApiOperation("Hello控制类")
@GetMapping(value = "/hello2")
public String hello2(@ApiParam("用户名") String username) {
return "hello" + username;
}
}
6.try it out
7. 总结
- 我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息。
- 接口文档实时更新
- 可以在线测试
【注意点】在正式发布的时候,关闭Swagger!!!出于安全考虑,而且节约运行内存。