一 配置基本信息

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();
}
}

Swagger 配置_接口文档

二 设置扫描的包

可以通过 apis()方法设置哪个包中内容被扫描

@Configuration
public class SwaggerConfig {
@Bean
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();
}
}

Swagger 配置_描述文件_02

三 自定义注解设置不需要生成接口文档的方法

3.1自定义注解



@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotIncludeSwagger {
}


3.2添加规则

通 过 public ApiSelectorBuilder apis(Predicate

selector)可以设置生成规则。

public static Predicate not(Predicate predicate) :表示不

允许的条件。

withMethodAnnotation:表示此注解是方法级别注解。

@Bean
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 进行生成在接口文档中

@RestController
@RequestMapping("/people")
public class DemoController {

@NotIncludeSwagger
@PostMapping("/getPeople")
public People getPeople(Long id, String name){
People peo = new People();
peo.setId(id);
peo.setName(name);
peo.setAddress("上海");
return peo;
}
}


Swagger 配置_描述文件_03