如何使用 Springfox 从 Swagger 文档中隐藏端点
How to hide endpoints from Swagger documentation with Springfox
我有一个 Spring 引导项目,下一个依赖项是 Springfox:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
我有我的界面:
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
@RestController
@RequestMapping(value = "/cache")
@ApiIgnore
@Api(hidden = true)
public interface CacheController {
@RequestMapping(
value = "clear/",
method = RequestMethod.GET,
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE}
)
@ApiOperation(value = "", hidden = true)
ResponseEntity<String> clearToken();
}
注解@ApiIgnore
和@Api(hidden = true)
(我已经分别测试过它们,它们也不起作用。)没有隐藏文档的效果。它仅在注释位于方法上方时才有效,但我想将它们全部隐藏,因为我还有其他要隐藏的端点。
一些想法?
编辑:
这是我的 Swagger 配置:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public static String API_KEY_NAME;
@Bean
public Docket apiDocumentation() {
List<ResponseMessage> errorList = this.defineResponseMessages();
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("my.package.rest"))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(true)
.globalResponseMessage(RequestMethod.GET, errorList)
.securitySchemes(Arrays.asList(this.apiKey()))
.securityContexts(Arrays.asList(this.securityContext()))
.apiInfo(this.apiInfo());
}
@Value("${server.security.apiKeyName}")
public void setApiKeyName(final String apiKeyName) {
SwaggerConfig.API_KEY_NAME = apiKeyName;
}
private ApiKey apiKey() {
return new ApiKey("apiKey", API_KEY_NAME, "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.any()).build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Arrays.asList(new SecurityReference("apiKey", authorizationScopes));
}
private List<ResponseMessage> defineResponseMessages() {
List<ResponseMessage> errorList = new ArrayList<ResponseMessage>();
ResponseMessage responseMessage = new ResponseMessageBuilder()
.code(HttpStatus.INTERNAL_SERVER_ERROR.value())
.message(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase())
.build();
errorList.add(responseMessage);
responseMessage = new ResponseMessageBuilder()
.code(HttpStatus.UNAUTHORIZED.value())
.message(HttpStatus.UNAUTHORIZED.getReasonPhrase())
.build();
errorList.add(responseMessage);
responseMessage = new ResponseMessageBuilder()
.code(HttpStatus.NOT_FOUND.value())
.message(HttpStatus.NOT_FOUND.getReasonPhrase())
.build();
errorList.add(responseMessage);
return errorList;
}
private ApiInfo apiInfo() {
ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
return apiInfoBuilder
.title("My API")
.description("Description")
.version("1.0.0 Beta")
.build();
}
}
您已在接口上添加了 @ApiIgnore
注释。看起来,这个注释在添加到界面上时不起作用。 (我真的不明白为什么 @Api
在接口上工作而 @ApiIgnore
不工作。)
直接将注释添加到您的控制器class。这应该可以解决您的问题。
@Api
注释上的 hidden
属性 当前不起作用。 (参见 this GitHub 问题。)
另一种方法是使用 @ApiOperation(hidden = true)
这个可以用在controller/handler级别的方法。
例如
@RestController
public HomeController{
@ApiOperation(value = "<Your Message>", hidden = true)
public String getMessage(@RequestParam(value = "msg") final String msg){
return msg;
}
}
另一种选择是完全删除@Api,您的控制器及其方法不应该被招摇过市。
我们只想从 class 中隐藏特定方法的场景。对于 swagger.v3,在 io.swagger.core.v3:swagger-annotations:2.0.10 jar
中有一个名为 Hidden
的注释。要隐藏的方法可以使用 Hidden
注释进行注释,如下所示。下面的方法显示了需要从 swagger 文档中隐藏的 DELETE
操作的方法。
@DELETE
@Hidden
public void deleteList(int id) {
//code goes here.
}
对于OpenAPI3和SpringBoot:
我在控制器的方法上使用了@Hidden 注释。
它似乎在方法级别和控制器级别都有效。
@隐藏注释是从使用导入的:
import io.swagger.v3.oas.annotations;
另一个不同的好方法是在 SpringFox 配置上定义可见路径
@Configuration
@EnableSwagger2
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.or(PathSelectors.ant("/rtm/**"), PathSelectors.ant("/appview/**")))
.build().apiInfo(apiEndPointsInfo());
}
}
这样您就可以集中定义可见路径并避免在许多控制器上放置 swagger 注释。
我有一个 Spring 引导项目,下一个依赖项是 Springfox:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
我有我的界面:
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
@RestController
@RequestMapping(value = "/cache")
@ApiIgnore
@Api(hidden = true)
public interface CacheController {
@RequestMapping(
value = "clear/",
method = RequestMethod.GET,
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE}
)
@ApiOperation(value = "", hidden = true)
ResponseEntity<String> clearToken();
}
注解@ApiIgnore
和@Api(hidden = true)
(我已经分别测试过它们,它们也不起作用。)没有隐藏文档的效果。它仅在注释位于方法上方时才有效,但我想将它们全部隐藏,因为我还有其他要隐藏的端点。
一些想法?
编辑:
这是我的 Swagger 配置:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public static String API_KEY_NAME;
@Bean
public Docket apiDocumentation() {
List<ResponseMessage> errorList = this.defineResponseMessages();
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("my.package.rest"))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(true)
.globalResponseMessage(RequestMethod.GET, errorList)
.securitySchemes(Arrays.asList(this.apiKey()))
.securityContexts(Arrays.asList(this.securityContext()))
.apiInfo(this.apiInfo());
}
@Value("${server.security.apiKeyName}")
public void setApiKeyName(final String apiKeyName) {
SwaggerConfig.API_KEY_NAME = apiKeyName;
}
private ApiKey apiKey() {
return new ApiKey("apiKey", API_KEY_NAME, "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.any()).build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Arrays.asList(new SecurityReference("apiKey", authorizationScopes));
}
private List<ResponseMessage> defineResponseMessages() {
List<ResponseMessage> errorList = new ArrayList<ResponseMessage>();
ResponseMessage responseMessage = new ResponseMessageBuilder()
.code(HttpStatus.INTERNAL_SERVER_ERROR.value())
.message(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase())
.build();
errorList.add(responseMessage);
responseMessage = new ResponseMessageBuilder()
.code(HttpStatus.UNAUTHORIZED.value())
.message(HttpStatus.UNAUTHORIZED.getReasonPhrase())
.build();
errorList.add(responseMessage);
responseMessage = new ResponseMessageBuilder()
.code(HttpStatus.NOT_FOUND.value())
.message(HttpStatus.NOT_FOUND.getReasonPhrase())
.build();
errorList.add(responseMessage);
return errorList;
}
private ApiInfo apiInfo() {
ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
return apiInfoBuilder
.title("My API")
.description("Description")
.version("1.0.0 Beta")
.build();
}
}
您已在接口上添加了 @ApiIgnore
注释。看起来,这个注释在添加到界面上时不起作用。 (我真的不明白为什么 @Api
在接口上工作而 @ApiIgnore
不工作。)
直接将注释添加到您的控制器class。这应该可以解决您的问题。
@Api
注释上的 hidden
属性 当前不起作用。 (参见 this GitHub 问题。)
另一种方法是使用 @ApiOperation(hidden = true)
这个可以用在controller/handler级别的方法。
例如
@RestController
public HomeController{
@ApiOperation(value = "<Your Message>", hidden = true)
public String getMessage(@RequestParam(value = "msg") final String msg){
return msg;
}
}
另一种选择是完全删除@Api,您的控制器及其方法不应该被招摇过市。
我们只想从 class 中隐藏特定方法的场景。对于 swagger.v3,在 io.swagger.core.v3:swagger-annotations:2.0.10 jar
中有一个名为 Hidden
的注释。要隐藏的方法可以使用 Hidden
注释进行注释,如下所示。下面的方法显示了需要从 swagger 文档中隐藏的 DELETE
操作的方法。
@DELETE
@Hidden
public void deleteList(int id) {
//code goes here.
}
对于OpenAPI3和SpringBoot:
我在控制器的方法上使用了@Hidden 注释。
它似乎在方法级别和控制器级别都有效。
@隐藏注释是从使用导入的:
import io.swagger.v3.oas.annotations;
另一个不同的好方法是在 SpringFox 配置上定义可见路径
@Configuration
@EnableSwagger2
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.or(PathSelectors.ant("/rtm/**"), PathSelectors.ant("/appview/**")))
.build().apiInfo(apiEndPointsInfo());
}
}
这样您就可以集中定义可见路径并避免在许多控制器上放置 swagger 注释。