项目需要 一些Controller不能在swagger中显示,网上搜了一下,只能配置扫描那些包,但是不能配置不扫描那些。
记录一下:
@EnableWebMvc
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@SuppressWarnings("unchecked")
@Bean
public Docket api() {
@SuppressWarnings("unchecked")
//指定的访问路径,才生成接口文档
Predicate<String> paths = Predicates.or(PathSelectors.ant("/common/codeType/**"),
PathSelectors.ant("/common/codeItem/**"),
PathSelectors.ant("/i18n/**")
);
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
//此包路径下的类,才生成接口文档
.apis(Predicates.or(RequestHandlerSelectors.basePackage("com.xx.controller"),
RequestHandlerSelectors.basePackage("com.cxc.common.code.controller")
)).paths(paths)
.build()
.securitySchemes(Collections.singletonList(apiKey()))
.securityContexts(Collections.singletonList(securityContext()));
}
private static ApiKey apiKey() {
return new ApiKey("token", "token", "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(Collections.singletonList(new SecurityReference("token", new AuthorizationScope[0])))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.description("**swagger")
.title("**swagger").build();
}
}