I've seen different approaches on how to either disable or enable swagger(eg: @Profile etc) but none are useful when you want to use swagger, you can just enable and disable it when done on a running application without restart.
1 Answers
There are ways to do it or workarounds, but no direct spring swagger configuration which you can use. I am gonna jot dot some options/steps I can think for you, which you can try.
Option 1
Create a
filterchain
for swagger URI, which will check, say a property nameswagger.enable
true/false
, accordingly forward or reject the request.Either expose a post endpoint to update this property using
env.setProperty
or keep this property inapplication.properties
& create reload feature. Some reference here for reload.
Option 2
Create your docket bean from property like below:
@Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .... .build().enable(env.getProperty("swagger.enable")); }
Ref
Option 1 : Step#2
Add logic to reinitialize the
Docket
bean based on trigger, may be as followup action of step#2 endpoint.
So your swagger URI will work based on current state of this property, which you are controlling, thus will not require restart.

- 4,400
- 1
- 21
- 28