Swagger
介绍
所需依赖
swagger集成spring boot
配置Swagger
配置环境
注意:spring boot没有集成swagger,导入swagger依赖后需要自己手动配置
所需依赖:
<!--这里使用spring boot 2.1.3版本的spring-boot-starter-parent。我之前选用2.6.1的导致空指针异常-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<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>
</dependency>
配置扫描接口及开关
禁用swagger在浏览器中使用
解决办法
分组和接口测试
配置组
问题:如何配置多个分组?
接口测试
使用swagger的优点
1.方便前后端的交流。
2.前端可以拿到详细的接口详细。
3.swagger可以配置分组(即多个人的接口信息)
4.可以实时更新。
5.可以为后端中的实体类和Controller书写详细的注释信息
使用案例
实体类配置
package com.example.swagger.com.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Component;
@ApiModel("User实体类信息")
@Component
public class User {
@ApiModelProperty("用户名")
private String user;
@ApiModelProperty("密码")
private String pass;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return "User{" +
"user='" + user + '\'' +
", pass='" + pass + '\'' +
'}';
}
}
swagger配置类
package com.example.swagger.com.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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 springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.beans.EventHandler;
import java.util.ArrayList;
/**
* 浏览器访问地址:http://localhost:8080/swagger-ui.html
*/
@Configuration
@EnableSwagger2 // 开启Swagger配置
public class SwaggerConfig {
// 配置Swagger的Docket的bean的实例
@Bean
public Docket docket_hjx(Environment environment){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("hjx") // 配置组
.apiInfo(MyApiInfo_hjx())
.enable(isStartSwagger(environment))
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.com.hjx"))
.paths(PathSelectors.ant("/**")) //swagger要拦截的请求地址
.build();
}
@Bean
public Docket docket_cyh(Environment environment){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("cyh")// 配置组
.enable(isStartSwagger(environment))
.apiInfo(MyApiInfo_cyh())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.com.hjx"))
.paths(PathSelectors.ant("/**")) //swagger要拦截的请求地址
.build();
}
@Bean
public Docket docket_yyy(Environment environment){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("yyy")// 配置组
.enable(isStartSwagger(environment))
.apiInfo(MyApiInfo_yyy())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.com.hjx"))
.paths(PathSelectors.ant("/**")) //swagger要拦截的请求地址
.build();
}
//hjx的接口文档
private ApiInfo MyApiInfo_hjx(){
// 作者信息
Contact contact=new Contact("hjx","http://baidu.com","2795348161@qq.com");
return new ApiInfo("hjx的Swagger文档测试案例",
"世上无难事,只怕有心人",
"v1.0",
"http://baidu.com",
contact,
"Apache 2.0",
"http://localhost",
new ArrayList());
}
private ApiInfo MyApiInfo_cyh(){
// 作者信息
Contact contact=new Contact("hjx","http://baidu.com","2795348161@qq.com");
return new ApiInfo("cyh的Swagger文档测试案例",
"阿豪,天下无敌",
"v1.0",
"http://baidu.com",
contact,
"Apache 2.0",
"http://localhost",
new ArrayList());
}
private ApiInfo MyApiInfo_yyy(){
// 作者信息
Contact contact=new Contact("yyy","http://baidu.com","2795348161@qq.com");
return new ApiInfo("yyy的Swagger文档测试案例",
"犹元勇,天下无敌",
"v1.0",
"http://baidu.com",
contact,
"Apache 2.0",
"http://localhost",
new ArrayList());
}
/**
* 动态设置 是否开启swagger
*/
public boolean isStartSwagger(Environment environment){
//设置要使用swagger的环境
Profiles profiles=Profiles.of("test","dev");
// 如果当前环境是dev或test则返回true
boolean flag= environment.acceptsProfiles(profiles);
System.out.println("falg="+flag);
System.out.println("当前开启的执行环境是:");
for(String str:environment.getActiveProfiles()){
System.out.println(str);
}
return flag;
}
}
Controller配置
package com.example.swagger.com.hjx;
import com.example.swagger.com.domain.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@ApiOperation("Hello控制接口")
@GetMapping("/hello")
public String Hello(@RequestParam("user") String user,@RequestParam("pass") String pass){
System.out.println("获取的user="+user+" pass="+pass);
return "user="+user+" pass="+pass;
}
@ApiOperation("获取user接口")
@GetMapping("/get/user")
// 注意:如果这里采用对象接收,一定要在实体类中配置 get/set方法 否者swagger中无法显示配置信息
public User get(User user){
System.out.println("user对象="+user);
//只要这里返回pojo对象(java对象),该对象就会扫描到swagger中
return user;
}
}
注意点
在项目上线后,需要将swagger关闭,以防暴露内容接口信息,出于安全考虑。还可以节约内存,提高程序效率。