swagger配置代码:
package com.wjj.application;
import com.wjj.application.response.ReturnCode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.BasicAuth;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* swagger配置
* Docket 排序方式(groupName): 1-10,a-z,A-Z
* @author hank
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket payDocket() {
return getDocket("a 支付相关", "com.wjj.application.controller", "1.支付相关", "1.支付相关",
new String[]{
"/manager/totalPay/.*",
"/payment/bindBankcard/.*"
}
);
}
@Bean
public Docket index18Docket() {
return getDocket("z 1.8接口汇总", "com.wjj.application.controller", "1.8接口汇总", "1.8接口汇总",
new String[]{
"/homeMall/getActivityGoods",
"/backGroundImage/imagesList",
"/tabBar/getClientTabBar",
"/homePage/iconsList",
"/marketintActive/seckill/activeGoodInfo",
"/wjjGmOpenToken/account/anchorGoodsSales/addAnchorGoodsSales",
"/wjjGmOpenToken/account/anchorGoodsSales/statisticsAnchorGoods",
"/wjjGmOpenToken/account/anchorGoodsSales/batchStatisticsAnchorGoodsNums",
"/wjjGmOpenToken/account/anchorGoodsSales/selectAnchorGoodsList",
"/wjjGmOpenToken/account/anchorGoodsSales/selectAnchorGoodsNums",
"/createLiveUser/selectLiveUserList",
"/wx/miniapps/shareGmhealthLive",
"/backGroundImage/v1.5/getPayBanner"
}
);
}
//manager group
@Bean
public Docket allDocket() {
return getDocket("z All", "com.wjj.application.controller", "后台接口", "后台接口");
}
private Docket getDocket(String groupName, String basePackage, String apiName, String apiDesc){
return getDocket(groupName, basePackage, apiName, apiDesc, null);
}
/**
*
* @param groupName 组名
* @param basePackage 扫描包路径
* @param apiName apiName
* @param apiDesc apiDesc
* @param paths 正则路径匹配
* @return
*/
private Docket getDocket(String groupName, String basePackage, String apiName, String apiDesc, String[] paths){
ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo(apiName, apiDesc))
.groupName(groupName)
.securitySchemes(Arrays.asList(new BasicAuth("test")))
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage));
if(paths==null){
apiSelectorBuilder.paths(PathSelectors.any());
}else{
StringBuilder pathRegex = new StringBuilder();
for (String path : paths){
pathRegex.append("(").append(path).append(")|");
}
apiSelectorBuilder.paths(PathSelectors.regex(pathRegex.substring(0, pathRegex.length()-1)));
}
return apiSelectorBuilder.build()
// .globalOperationParameters(getPubParam())
.globalResponseMessage(RequestMethod.GET, getResponseMessage())
.globalResponseMessage(RequestMethod.POST, getResponseMessage())
;
}
private ApiInfo apiInfo(String title, String description) {
return new ApiInfoBuilder()
.title(title)
.description(description)
.version("1.0").build();
}
/**
* 获得公共的参数
* @return
*/
private List getPubParam() {
ParameterBuilder tokenPar = new ParameterBuilder();
List pars = new ArrayList<>();
tokenPar.name("X-Auth-Token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return pars;
}
private List getResponseMessage(){
List responseMessageList = new ArrayList<>();
responseMessageList.add(new ResponseMessageBuilder().code(Integer.valueOf(ReturnCode.SUCCESS.getCode())).message("成功").build());
responseMessageList.add(new ResponseMessageBuilder().code(Integer.valueOf(ReturnCode.NO_TOKEN.getCode())).message("登录失效").build());
responseMessageList.add(new ResponseMessageBuilder().code(3002).message("权限不足").build());
return responseMessageList;
}
}
这个bean就是一个分组:
@Bean
public Docket payDocket() {
return getDocket("a 支付相关", "com.wjj.application.controller", "1.支付相关", "1.支付相关",
new String[]{
"/manager/totalPay/.*",
"/payment/bindBankcard/.*"
}
);
}
这里要注意,swagger的分组列表排序默认是按照groupName来排序的这个比较坑,Docket 排序方式(groupName): 1-10,a-z,A-Z,所以命名Docket的groupName时候要注意顺序的处理
最后的效果是: