Search code examples
springdocspringdoc-openapi-uispringdoc-ui

How to hide endpoints based on Profiles in Spring Boot and Spring doc?


I am working on Spring Boot v2.2.2.RELEASE and SpringDoc UI and Open API Specification OAS3 and I found very related question here: https://github.com/springdoc/springdoc-openapi/issues/201.

I've 4 Profiles say Dev, Stage, UAT and Prod and say I've Student API, Employee API and Department API.

I want for UAT and Prod profiles, I want to hide Department API. How can we do not ?


Solution

  • You can use groups: Declare each of your API in groups.

    And, add the @Profile annotation together with @Bean annotation for the group definition: This will help you display the OpenAPI spec depending on your spring profiles

    @Bean
    @Profile("!prod")
    public GroupedOpenApi actuatorApi() {
        return GroupedOpenApi.builder().group("Actuator")
                .pathsToMatch("/actuator/**")
                .pathsToExclude("/actuator/health/*")
                .build();
    }