ConditionalOnProperties不会在具有多个端点Spring Boot的类中切换端点

问题描述 投票:1回答:1

我尝试使用ConditionalOnProperties注释来查找具有多个端点的类中的特定端点。但是,条件似乎没有切换,但始终打开。它在类级别上运行良好,但在端点级别不运行。这是一个错误吗?

@RequestMapping(path = "/test", consumes = {"application/x-www-form-urlencoded"})
@ResponseBody
@Timed()
@ConditionalOnProperty(name = "test.enabled")
public String test(@RequestParam(EXCEPTION_LOG_MESSAGE) String errorLog) {
spring spring-boot annotations toggle endpoint
1个回答
1
投票

据我理解注释,它应该用于bean。要么是一个方法,它返回一个@Bean或一个类,这是一个@Component@Sevice或-as在你的情况下 - 一个@Controller

您正在注释的方法没有定义bean,而只是bean的一个方法,无论如何都会被定义。

为了实现您的目标,您可以举例说明

  • 将特定端点放到额外的Controller并注释一个
  • 或使用@Value注释获取属性,只需添加一个if到你的方法,使其返回类似404的东西,以防属性未设置:

后一个想法的例子:

@Value("${test.enabled}")
private boolean testEnabled;

public ResponseEntity test() {
    if (!testEnabled) {
        return ResponseEntity.notFound().build();
    }
    // ...
}

可能有更多选择。

© www.soinside.com 2019 - 2024. All rights reserved.