Given a "standard" spring boot application with a @RestController
, eg
@RestController
@RequestMapping(value = "foo", produces = "application/json;charset=UTF-8")
public class MyController {
@RequestMapping(value = "bar")
public ResponseEntity<String> bar(
return new ResponseEntity<>("Hello world", HttpStatus.OK);
}
}
Is there an annotation or technique that prevents the endpoint from starting at all if/unless a certain application property exists/doesn't exist.
Note: Testing a property inside the method and exploding is not a solution, because the endpoint will exist.
I don't care about the granularity: ie enabling/disabling just a method or the whole class are both fine.
Because a profile is not a property, control via profiles does not solve my problem.
I found a simple solution using @ConditionalOnExpression
:
@RestController
@ConditionalOnExpression("${my.controller.enabled:false}")
@RequestMapping(value = "foo", produces = "application/json;charset=UTF-8")
public class MyController {
@RequestMapping(value = "bar")
public ResponseEntity<String> bar(
return new ResponseEntity<>("Hello world", HttpStatus.OK);
}
}
With this annotation added, unless I have
my.controller.enabled=true
in my application.properties
file, the controller won't start at all.
You can also use the more convenient:
@ConditionalOnProperty("my.property")
Which behaves exactly as above; if the property is present and "true"
, the component starts, otherwise it doesn't.