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

 

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.

 

相关文章:

  • 2021-07-06
  • 2021-10-30
  • 2021-12-25
  • 2022-12-23
  • 2021-06-30
  • 2022-02-05
  • 2021-05-29
  • 2021-07-22
猜你喜欢
  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
  • 2022-02-01
  • 2022-12-23
  • 2021-05-03
  • 2021-07-03
相关资源
相似解决方案