Yogen Rai
Yogen Rai

Reputation: 3033

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

Upvotes: 6

Views: 10604

Answers (3)

Uncle Iroh
Uncle Iroh

Reputation: 6055

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

Upvotes: 0

Yogen Rai
Yogen Rai

Reputation: 3033

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.

Upvotes: 16

satish chennupati
satish chennupati

Reputation: 2650

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

Upvotes: 1

Related Questions