我正在使用以下类实现自定义端点:
@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/