一:可在项目中创建SwaggerConfig配置类,对文档详细信息进行配置
//swagger配置类,用于配置swagger的详细信息,比如标题、网站、邮箱
@Configuration
public class SwaggerConfig {
/**
* 返回docket对象描述
* @return
*/
@Bean
public Docket getDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(swaggerDemoApiInfo())
.select()
.build();
}
/**
* 返回信息描述的对象
* @return
*/
private ApiInfo swaggerDemoApiInfo(){
return new ApiInfoBuilder()
.contact(new Contact("杨露","http:www.baidu.com","2568673349@qq.com"))
.title("这里是swagger的标题")
.description("这里是swagger的描述")
.version("1.0.0")
.build();
}
}
效果如下:
二:配置,哪些不需要生成接口文档的配置
1.配置扫描的包路径,设置哪个包中的内容被扫描
@Bean
public Docket getDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(swaggerDemoApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springswaggerdemo.controller"))
.apis(not(withMethodAnnotation(NotIncludeSwagger.class)))
.build();
}
2.创建自定义注解,自定义注解设置不需要生成接口文档的方法
//自定义注解 @Target({ElementType.METHOD})//这个注解是针对类的还是针对方法的 @Retention(RetentionPolicy.RUNTIME)//在运行时来调用这个注解 public @interface NotIncludeSwagger { }
2.在docket里面把这个注解配置进去,被这个注解修饰的,就跳过
@Bean
public Docket getDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(swaggerDemoApiInfo())
.select()
.apis(not(withMethodAnnotation(NotIncludeSwagger.class)))
.build();
}
三:设置满足什么样规则的url可以生成接口文档,可以使用正则表达式进行匹配。
下面的例子表示只有以/people/和/person/开头的url才能生成接口文档
@Bean
public Docket getDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(swaggerDemoApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springswaggerdemo.controller"))
.apis(not(withMethodAnnotation(NotIncludeSwagger.class)))
.paths(or(PathSelectors.regex("/people/.*"), PathSelectors.regex("/person/.*")))
.build();
}