spring - 如何在 Spring Boot 2.0 执行器端点的 `@Selector` 中使用 `@WriteOperation` ?

标签 spring spring-boot spring-boot-actuator

我正在使用以下类实现自定义端点:

@Component
@Endpoint(id = "bootstrap")
public class BootstrapUrlEndpoint {

  private final URL bootstrapUrl;

  @Autowired
  public BootstrapUrlEndpoint(URL bootstrapUrl) {
    this.bootstrapUrl = bootstrapUrl;
  }

  @ReadOperation
  public Map<String, String> getBootstrapUrl() {
    Map<String, String> result = new HashMap<>();
    result.put("bootstrap_url", bootstrapUrl.toExternalForm());
    return result;
  }

  @WriteOperation
  public void setBootstrapUrl(@Selector String property, String value) throws MalformedURLException {
    System.out.println(String.format(">>> Setting  %s = %s", property, value));
  }
}

这一切都“按预期工作”没有@Selector注释;省略它并将 POST 发送到 http://localhost:8080/actuator/bootstrap ,其中:

{
  "value": "http://localhost:27017/tables/application"
}

按预期调用该方法。

但是,我无法使“选择器”工作;我在启动日志中看到它已注册为有效端点:

Mapped "{[/actuator/bootstrap/{arg0}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public org.reactivestreams....ava.util.Map<java.lang.String, java.lang.String>)

不幸的是,使用 POST/actuator/bootstrap/myprop 和相同的正文调用它会产生 400 Bad Request ,没有错误日志或错误消息。

我一直在寻找更多信息和可能的示例:我能找到的唯一相关(但是,唉,不完整)的示例是 this article - 有人知道我的代码中缺少什么吗?

提前致谢!

最佳答案

顺便说一句,我刚刚遇到了和你一样的问题,而且有点疯狂。

但我发现问题与@Selector注释的参数的参数命名有关。

如果您将 var“property”命名为“arg0”,则所有操作都会起作用,如下所示:

public void setBootstrapUrl(@Selector String arg0, String value)

是的,我知道这有点奇怪,但我在这个 article 中找到了一些信息谈论编译类时嵌入参数。

我仍然没有真正在参数中使用我自己的名字来完成这项工作。

关于spring - 如何在 Spring Boot 2.0 执行器端点的 `@Selector` 中使用 `@WriteOperation` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47920201/

相关文章:

java - 为执行器端点定义不同上下文的正确/最干净的方法是什么?

java - 与直接使用包装的 Twitter 等库相比,SpringSocial 为我带来了哪些额外好处

java - spring boot拦截器,拦截返回中文乱码的问题

spring - 我应该将 Spring applicationContext.xml 保存在 EAR 中的哪里

java - 在这种情况下,CommandLineRunner 做了什么?

spring - 从Spring依赖管理插件中提取依赖版本

java - @JsonSerialize 没有从 Controller springboot 2.2.4 转换我的日期格式

spring-mvc - Spring Boot 集成测试 : @AutoConfigureMockMvc and context caching

Spring 启动执行器 : Aggregation of/health endpoints cross micro-services