Swagger 2 @ApiIgnore 属性在配置服务器中切换 [英] Swagger 2 @ApiIgnore properties toggle in config server
问题描述
我在 Sprinngboot 应用程序中使用 swagger 2 (2.9.2).
I am using swagger 2 (2.9.2) in Sprinngboot app.
依赖:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
代码:
package com.khan.vaquar.swagger;
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Predicate;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket postsApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("vaquar khan public-api").apiInfo(apiInfo()).select().apis( RequestHandlerSelectors.basePackage( "com.khan.vaquar" ) )
.paths(paths()).build();
}
private Predicate<String> paths() {
return or(regex("/.*"), regex("/.*"));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("vaquar khan public-api")
.description("vaquar khan public-api app API reference for developers")
.termsOfServiceUrl("XXX-YYY-ZZZ.com").contact("info@vkhan.com")
.license("vaquar khan License").licenseUrl("Licence@vkhan.com").version("1.0").build();
}
}
我有将近 70 个不同的微服务,很多是内部的,只有少数 10 个微服务是外部的.
I have almost 70 different micro service , many are internal and only few 10 micro service are external .
现在在 swagger doc 中使用 @ApiIgnore 隐藏微服务,我们在 swagger 中忽略了 60,只显示了 10 个外部 api.
Now inside swagger doc hiding micro service using @ApiIgnore ,we ignored 60 in swagger and only displaying 10 external api.
问题:
现在我要求外部用户只能在 swagger doc 中看到 10 个微服务,而内部微服务 swagger (70) 应该对开发人员和内部用户可见.
Now i have requirement that external user can see only 10 micro service in swagger doc and internal micro service swagger (70) should visible to developers and internal users .
有什么方法可以在应用程序属性中定义@ApiIgnore for prod 并仅显示 10,而在开发配置属性中将隐藏@ApiIgnore
Is there any way we can define in application properties @ApiIgnore for prod and display only 10 and in dev config properties will hide @ApiIgnore
推荐答案
如果您使用配置文件,那么您就不需要 @ApiIgnore
注释.
If you go with profiles then you don't need the @ApiIgnore
annotation(s).
定义开发人员配置文件.例如:
Define a developer profile. For example:
@Profile("dev")
@Controller
@RequestMapping("/internal/...")
@Api(value = "/internal/...", description = "Internal stuff")
public class OneOfTheInternalControllers { ... }
现在用配置文件标记所有内部控制器/端点.最后,如果您在生产模式下运行应用程序,则无需执行任何操作.@Profile("dev")
API 将被隐藏.但是,如果您在本地或在测试环境中运行您的应用程序,请传递以下 JVM 参数:
Now mark all your internal controllers / endpoints with the profile. Finally if you run your application in production mode you don't have to do anything. The @Profile("dev")
APIs will be hidden. However if you run your application locally or in testing environment, pass the following JVM argument:
-Dspring.profiles.active=dev
如果您使用的是 IntelliJ,您可以在运行/调试配置中传递它:
If you are using IntelliJ you can pass it in Run/Debug Configurations:
最后,您应该会看到作为开发人员的所有 API.
At the end you should see all your APIs as a developer.
这篇关于Swagger 2 @ApiIgnore 属性在配置服务器中切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!