Disable Springdoc OpenAPI Swagger in production

Disable Springdoc OpenAPI Swagger in Production

Previously, we covered how to disable Swagger-ui in production using the SpringFox Swagger2 library. Unfortunately, that library is unmaintained and it is no longer possible to use it with the latest versions of Spring Boot. The best drop-in replacement option is the Springdoc-openapi library which we covered how to use in our Spring Boot Swagger documentation using OpenAPI article. In this article, we focus on how to disable Springdoc OpenAPI Swagger in the production environment on runtime.

Compared to Springfox 2, disabling API documentation in Springdoc OpenAPI is much simpler as it provides two out-of-the-box options. They should be placed in the application.properties or the application.yml file.

The first option is springdoc.swagger-ui.enabled, which allows us only enable to disable the Swagger UI. That means hitting the /v3/api-docs endpoint still will generate the API documentation.

To disable the Swagger UI, use the following property,

springdoc.swagger-ui.enabled=false

The second option is springdoc.api-docs.enabled, which allows us to control the API documentation generation at the /v3/api-docs endpoint. To disable, set the following property,

springdoc.api-docs.enabled=false

In case you want to disable both Swagger UI and the documentation endpoint, use both aforementioned properties together. That is the recommended approach.

Conclusion

In this article, we covered how to disable Springdoc OpenAPI Swagger in the production environment. In contrast to the now discontinued Springfox 2 library, we do not need to create a separate profile to disable the Swagger. We can achieve the same result using two properties provided by the Springdoc OpenAPI library.

As always, the fully functional demo project is available on GitHub at the following link:

https://github.com/kasramp/spring-kafka-test/

Inline/featured images credits