SwaggerConfig.java
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public static final String AUTHORIZATION_HEADER = "Authorization";
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).securitySchemes(Arrays.asList(apiKey())).select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).paths(PathSelectors.any()).
build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("API").description("DEMO").version("v1").build();
}
private ApiKey apiKey() {
return new ApiKey("JWT", AUTHORIZATION_HEADER, "header");
}
}
Run Code Online (Sandbox Code Playgroud)
此配置显示所有控制器,如何在 Swagger 上仅显示特定控制器?
1. 同一封装中的特定控制器
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).securitySchemes(Arrays.asList(apiKey())).select()
.apis(RequestHandlerSelectors.basePackage("com.tm.x.y.z.your.controller")).paths(PathSelectors.any()).
build().apiInfo(apiInfo());
}
Run Code Online (Sandbox Code Playgroud)
显示 com.tm.xyzyour.controller 包中的所有控制器
2. 不同封装中的特定控制器
创建类注释(例如:ShowAPI 注释)
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ShowAPI {
String value() default "";
}
Run Code Online (Sandbox Code Playgroud)
给控制器添加注解
@RestController
@ShowAPI
public class ExampleController {
}
Run Code Online (Sandbox Code Playgroud)
更改招摇配置
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).securitySchemes(Arrays.asList(apiKey())).select()
.apis(RequestHandlerSelectors.withClassAnnotation(ShowAPI.class)).paths(PathSelectors.any()).build()
.apiInfo(apiInfo());
}
Run Code Online (Sandbox Code Playgroud)
显示所有带有类注释的控制器是ShowAPI
| 归档时间: |
|
| 查看次数: |
5119 次 |
| 最近记录: |