Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide/remove Spring MVC endpoint in Swagger2

I'm using Swagger 2 for API UI. So, my gradle.build has:

compile "io.springfox:springfox-swagger2:${swaggerVersion}"
compile "io.springfox:springfox-swagger-ui:${swaggerVersion}"

I've configured Swagger as below:

@Configuration
@Profile("!production")
@EnableSwagger2
@ComponentScan(basePackageClasses = com.company.controllers.ContentController.class)
public class SwaggerConfiguration {
    @Autowired
    private BuildInfo buildInfo;

    @Bean
    public Docket awesomeApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.awesomeApiInfo())
                .select()
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
                .build();

    }

    private ApiInfo awesomeApiInfo() {
        return new ApiInfoBuilder()
                .title("Awesome API - build #" + this.buildInfo.getVersion())
                .description("Enter the IDs in order to look for the content")
                .version("0.1")
                .build();
    }
}

I'm getting the api endpoint that I have defined, but also getting the Spring MVC endpoints as below:

enter image description here

Now, I need to get rid of these mvc endpoints.

Any help is highly appreciated!!

like image 356
Yogen Rai Avatar asked Jan 31 '17 23:01

Yogen Rai


People also ask

How do you hide entity in Swagger?

To hide the "Models" section, add defaultModelsExpandDepth: -1 to the Swagger UI configuration code in your index.

How do I ignore API in Swagger?

By adding this attribute on a controller or action and specifying IgnoreApi = true , it gets hidden from auto-generated documentation.

How do I remove API from Swagger spring boot?

You can use the @Hidden annotation from swagger-annotations, on the top of the controller you want to hide. Save this answer. Show activity on this post. You can remove @EnableSwagger2 from your swagger config file or openapi config file then these openapi-resource controller will authomatically be removed.


2 Answers

Ohhh... actually it was my silly mistake. I changed RequestHandlerSelectors to select only endpoints from my own controller package as follow:

 @Bean
    public Docket awesomeApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.awesomeApiInfo())
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.company.awesome.controllers"))
                .build();

    }

And this shows only the endpoints mapped within the classes in controller package.

like image 81
Yogen Rai Avatar answered Sep 24 '22 02:09

Yogen Rai


The best approach you can follow is to restrict visibility and access to ServiceStack. So you can hide it from being visible externally with:

[Restrict(VisibleInternalOnly = true)]
public class InternalAdmin { }

you can read more about it here

like image 24
satish chennupati Avatar answered Sep 22 '22 02:09

satish chennupati