0

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.

Yushin
  • 1,684
  • 3
  • 20
  • 36
skanthala
  • 31
  • 4

1 Answers1

1

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

  1. Create a filterchain for swagger URI, which will check, say a property name swagger.enable true/false, accordingly forward or reject the request.

  2. Either expose a post endpoint to update this property using env.setProperty or keep this property in application.properties & create reload feature. Some reference here for reload.

Option 2

  1. Create your docket bean from property like below:

    @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .... .build().enable(env.getProperty("swagger.enable")); }

  2. Ref Option 1 : Step#2

  3. 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.

Amith Kumar
  • 4,400
  • 1
  • 21
  • 28