RestController 能不能經過配置關閉

https://stackoverflow.com/questions/29958231/can-a-spring-boot-restcontroller-be-enabled-disabled-using-propertiesjava

 

I found a simple solution using @ConditionalOnExpression:spring

@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 havejson

my.controller.enabled=true

in my application.properties file, the controller won't start at all.app

You can also use the more convenient:less

@ConditionalOnProperty("my.property")

Which behaves exactly as above; if the property is present and "true", the component starts, otherwise it doesn't.spring-boot

相關文章
相關標籤/搜索