一 配置基本信息
Docket:摘要对象,通过对象配置描述文件的信息。
apiInfo:设置描述文件中 info。参数类型 ApiInfo
select():返回 ApiSelectorBuilder 对象,通过对象调用 build()可以
创建 Docket 对象
ApiInfoBuilder:ApiInfo 构建器。
@Configuration
public class SwaggerConfig {
@Bean
public Docket getDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.build();
}
private ApiInfo getApiInfo(){
return new ApiInfoBuilder()
//文档标题
.title("第一个swagger的标题")
//文档描述
.description("这里是描述")
//文档版本
.version("1.0.0")
.contact(new Contact("上海迪士尼","http://www.baidu.com","22@com"))
.build();
}
}
二 设置扫描的包
可以通过 apis()方法设置哪个包中内容被扫描
public class SwaggerConfig {
public Docket getDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bjsxt.controller"))
.build();
}
private ApiInfo getApiInfo(){
return new ApiInfoBuilder()
//文档标题
.title("第一个swagger的标题")
//文档描述
.description("这里是描述")
//文档版本
.version("1.0.0")
.contact(new Contact("上海迪士尼","http://www.baidu.com","22@com"))
.build();
}
}
三 自定义注解设置不需要生成接口文档的方法
3.1自定义注解
ElementType.METHOD})({
(RetentionPolicy.RUNTIME)
public @interface NotIncludeSwagger {
}
3.2添加规则
通 过 public ApiSelectorBuilder apis(Predicate
selector)可以设置生成规则。
public static Predicate not(Predicate predicate) :表示不
允许的条件。
withMethodAnnotation:表示此注解是方法级别注解。
public Docket getDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bjsxt.controller"))
.apis(not(withMethodAnnotation(NotIncludeSwagger.class)))
.build();
}
3.3添加 NotIncludeSwagger 注解
在不需要生成接口文档的方法上面添加@NotIncludeSwagger 注
解后,该方法将不会被 Swagger 进行生成在接口文档中
("/people")
public class DemoController {
("/getPeople")
public People getPeople(Long id, String name){
People peo = new People();
peo.setId(id);
peo.setName(name);
peo.setAddress("上海");
return peo;
}
}