@Bean
public Docket apiDocument() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("api")
.select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(regex("/api.*"))
.build()
.pathMapping("/")
.globalOperationParameters(setHeaderToken())
.apiInfo(metadata());
}
不需要token验证的
@Bean
public Docket pubDocument() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("pub")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(regex("/pub.*"))
.build()
.pathMapping("/")
.apiInfo(metadata());
}
主要添加 globalOperationParameters(setHeaderToken())
private List<Parameter> setHeaderToken() {
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> 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;
}