Three Practical Ways to Disable Swagger2 in Spring Boot Production

This guide presents three concrete methods—property‑based toggling, profile‑driven configuration, and conditional resource handling—to safely prevent Swagger2 from being exposed in production environments of Spring Boot applications, helping you keep API documentation internal while maintaining flexibility across dev and test stages.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Three Practical Ways to Disable Swagger2 in Spring Boot Production

In a previous article we showed how to integrate Swagger2 with Spring Boot and control its access, but exposing Swagger UI in production is undesirable. The following three solutions let you disable Swagger2 selectively based on the deployment environment.

Solution 1: Conditional Property

Define a boolean flag in the configuration file, for example swagger.show: false for production and swagger.show: true for development or testing. Inject the flag into the Swagger configuration class with @Value("${swagger.show}") and pass it to the Docket builder via .enable(swaggerShow).

swagger:
  show: false
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${swagger.show}")
    private boolean swaggerShow;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .apiInfo(apiInfo());
    }
}

Solution 2: Profile‑Based Configuration

Leverage Spring’s @Profile annotation to load the Swagger configuration only in specific environments. By annotating the configuration class with @Profile({"dev","test"}), the bean is instantiated for development and testing, while it is ignored in production.

@Configuration
@EnableSwagger2
@Profile({"dev","test"})
public class SwaggerConfig {
    // other Swagger beans
}

Solution 3: Conditional Resource Handlers or Path Selectors

Extend WebMvcConfigurerAdapter (or implement WebMvcConfigurer) and conditionally register the Swagger UI resources based on the same flag. Alternatively, modify the Docket’s path selector to return PathSelectors.none() in production, effectively hiding all endpoints.

@Configuration
@ComponentScan("org.xx.interceptor")
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Value("${swagger.show}")
    private boolean swaggerShow;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (this.swaggerShow) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }
}
.paths(PathSelectors.none())

Conclusion

Pick the approach that best fits your project’s structure. The profile‑based method is generally preferred because it relies on Spring Boot’s native features, requires no extra bean initialization, and offers a clean, safe way to keep Swagger2 out of production. The property‑based toggle is a solid alternative when finer‑grained control is needed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaConfigurationSpring BootproductionSwagger2
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.