Hide/remove Spring MVC endpoint in Swagger2
Asked Answered
Q

3

6

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

Quadrennial answered 31/1, 2017 at 23:11 Comment(0)
Q
16

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.

Quadrennial answered 1/2, 2017 at 14:30 Comment(0)
L
1

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

Lilybel answered 1/2, 2017 at 1:54 Comment(2)
how can I restrict visibility for spring mvc endpoint?? I have attached screenshot on my question ..Quadrennial
if you have any yaml file you can set private : true for the end points you want to restrict.Lilybel
G
0

An alternative to specifying base package is to create a class annotation like this:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface SwaggerDocumentation {
}

and then once defined use it on your Controller as desired:

@RestController
@SwaggerDocumentation
public class EntityRestController {

    EntityService entityService;


    @Autowired
    public EntityRestController(final EntityService entityService) {
        this.entityService = entityService;
    }

    @GetMapping("/status")
    String getTest() {
        return "Ready";
    }

    @GetMapping("/api/entities")
    Collection<Entity> getEntities() {
        return entityService.findSome();
    }

}

and then finally in the SwaggerConfig class

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(SwaggerDocumentation.class))
                .build();
    }
}
Grovergroves answered 26/7, 2019 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.