swagger
springboot集成swagger
依赖
<!-- 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>
创建配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
测试
http://localhost:8080/swagger-ui.html
配置swagger
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket customDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true) //是否启用Swagger
.select()
//配置扫描接口的方式
//any所有
//none不扫描
//withMethodAnnotation 方法上注解扫描
//withClassAnnotation 类上注解扫描
//basePackage 包扫描
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//扫描的包路径
//过滤路径
//regex正则
//ant路径
//any
//none
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
Contact contact = new Contact("刘福成","https://blog.csdn.net/weixin_45742032","823613050@qq.com");
return new ApiInfo("刘福成的swagger Api", //
"随便写个座右铭",
"1.0",
"https://blog.csdn.net/weixin_45742032",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
怎么只在开发环境使用swagger
新建application-dev.properties,application-pro.properties两个文件,设置端口分别为8081,8082
设置项目环境
修改SwaggerConfig
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket customDocket(Environment environment) {
//项目环境是dev或test时启用Swagger
Profiles of = Profiles.of("dev", "test");
boolean b = environment.acceptsProfiles(of);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(b); //是否启用Swagger
}
private ApiInfo apiInfo() {
Contact contact = new Contact("刘福成","https://blog.csdn.net/weixin_45742032","823613050@qq.com");
return new ApiInfo("刘福成的swagger Api", //
"随便写个座右铭",
"1.0",
"https://blog.csdn.net/weixin_45742032",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
开发环境测试
正式环境测试
将项目环境设置为正式环境
正式环境无法使用Swagger,设置成功
swagger分组
swagger分组在项目开发中可以设置为开发人员,可以对不同的组进行不同的设置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket customDocket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).groupName("张三");
}
@Bean
public Docket customDocket2(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).groupName("李四");
}
private ApiInfo apiInfo() {
Contact contact = new Contact("刘福成","https://blog.csdn.net/weixin_45742032","823613050@qq.com");
return new ApiInfo("刘福成的swagger Api", //
"随便写个座右铭",
"1.0",
"https://blog.csdn.net/weixin_45742032",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
注释
model设置:当controller的返回值是指定对象时model就会被设置
@RestController
@RequestMapping("user")
public class UserController {
@PostMapping("/findUser1")
@ApiOperation("查询用户")
public UserInfo findUser1(String uname,int uage){
UserInfo userInfo = new UserInfo();
userInfo.setUname(uname);
userInfo.setUage(uage);
return userInfo;
}
@GetMapping ("/findUser2")
public Object findUser2(){
UserInfo userInfo = new UserInfo();
return userInfo;
}
}
设置注解
@ApiModel("用户")
public class UserInfo {
private int id;
@ApiModelProperty("用户名")
private String uname;
@ApiModelProperty("年龄")
private int uage;
private Department department;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getUage() {
return uage;
}
public void setUage(int uage) {
this.uage = uage;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
结果展示
UserInfo 的注释都展示出来了
UserController 控制层的注释展示出来了。