Another way to do this might be an easier way to do this: use the @ConditionalOnProperty annotation using RestController / Controller.
@RestController("api.test") @ConditionalOnProperty(name = "testcontroller.enabled", havingValue = "true") public class TestController { @RequestMapping(value = "/test", method = RequestMethod.GET) public String test(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
Here, the testcontroller.enabled property in your yml properties says that if it is not set to true, the TestController Bean is never created.
Tip . I suggest you use RestController instead of Controller, as @ResponseBody is added by default. You can use @ConditionalOnExpression to arrive at the same solution, but a bit slower due to the SpEL score.
source share