Swagger 2 @ApiIgnore properties toggle in config server

ε祈祈猫儿з 提交于 2020-01-25 03:59:25

问题


I am using swagger 2 (2.9.2) in Sprinngboot app.

Dependency :

<!-- 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>

Code :

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

}

I have almost 70 different micro service , many are internal and only few 10 micro service are external .

Now inside swagger doc hiding micro service using @ApiIgnore ,we ignored 60 in swagger and only displaying 10 external api.

Problem:

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 .

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


回答1:


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 { ... }

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

If you are using IntelliJ you can pass it in Run/Debug Configurations:

At the end you should see all your APIs as a developer.



来源:https://stackoverflow.com/questions/58466951/swagger-2-apiignore-properties-toggle-in-config-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!