文章目录
前言
关于swagger的分组注释
提示:以下是本篇文章正文内容
swagger的分组注释
- 通过配置两个Docket对Api文档进行分组,设置相关显示及扫描的信息
package com.warrior.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 springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration//配置类的注解
@EnableSwagger2//开启swagger2
public class SwaggerConfig {
@Bean
public Docket docket1(){
//分组显示不同配置下得文件
return new Docket(DocumentationType.SWAGGER_2)
.groupName("warrior")
.apiInfo(apiInfo_warrior())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/user/**"))//扫描这个路径下的接口文件
.build();
}
//管理前端页面的文档显示
@Bean
public Docket docket(Environment environment){
//获取项目环境:Environment environment
//设置要显示的swagger环境
Profiles profiles = Profiles.of("dev");
//通过environment.acceptsProfiles(profiles)判断自己是否处在设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo_warrior_l())
.groupName("warrior_l")
//enable:false关闭,true开启
// .enable(flag)
.select()
//RequestHandlerSelectors,配置扫描接口的方式
//basePackage指定扫描的包
.apis(RequestHandlerSelectors.any())
//paths:过滤器扫描上边包里有下面的路径
// .paths(PathSelectors.ant("/student/**"))
//扫描有这个接口的接口
.build();
}
//作者信息
Contact contact = new Contact("warrior", "http://localhost:8080/", "zghblwz@163.com");
//配置swagger信息=apiInfo
private ApiInfo apiInfo_warrior_l(){
return new ApiInfo("warrior", "description", "version", "http://localhost:8080", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
private ApiInfo apiInfo_warrior(){
return new ApiInfo("warrior的Api文档", "测试api的分组注释内容","1.0", "http://localhost:8080",contact
,"Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
- 实体类信息的注释
- 几个注释
package com.warrior.pojo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel("用户实体类") //对api文档的内容进行一个注释说明 public class User { //定义一个实体类,使接口返回这个实体类 @ApiModelProperty("用户名")//注释参数的含义 String username; @ApiModelProperty("密码") String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
- 相关显示结果
总结
swagger的优点:1. 可以通过swagger给一些比较难理解的属性或者接口,增加注释信息
2. 接口文档实时更新
3. 可以在线测试
【注意点】
在发布的时候,关闭swagger!!!!