Disable swagger in spring mvc

I am new to Swagger and have implemented Swagger frontend with spring mvc, I want to disable UI in production / live environment. I am struggling to figure this out.

This is what I am using

SwaggerConfig.java

@Configuration
@EnableSwagger2
public class SwaggerConfig {


}

      

RestController.java

    @RestController
    @Api(value="city", description="Operations pertaining to City Data")
    @RequestMapping(value="/v1/city")
    public class RestController {

    @ApiOperation(value = "View city by stateId or stateName")
        @RequestMapping(value="/search",method=RequestMethod.POST)
        public ResponseEntity<Object> getCityBystateId(@RequestBody StateDto stateDto,Model model){
        }
}

      

+3


source to share


2 answers


Another way is to use a reverse proxy to deny access to the Swagger Api in production.



In this case, your production setup is exactly the same as your dev / test environment (then more DevOps-compatible) and you can continue to access your Swagger API with internal calls.

+5


source


Have a look at the Spring profile mechanism that allows you to register different beans in different environments

When you load it as per the doc, you can annotate your swagger config class at the class level, eg. @Profile("dev")

thus allowing swagger setup for your chosen environment



@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {


}

      

+7


source







All Articles