jboss 如何通过REST在WildFly中进行系统属性操作?

toiithl6  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(109)

本文档说明用户可以通过REST对WildFly服务器执行某些操作:https://docs.jboss.org/author/display/WFLY10/The%20HTTP%20management%20API.html然而,没有关于如何添加/删除/读取系统属性的示例。我不知道HTTP主体如何查找这些调用。
以下StackOverflow问题的答案表明示例中使用的SimpleOperation类实际上并不存在:Wildfly 10 management Rest API
我想做如下操作:

/system-property=BLA:remove
/system-property=BLA:add(value="1,2,3,4")

并阅读它。
我如何通过REST和WildFly HTTP管理API执行这些操作?理想情况下,我会使用Java API(如果有)。

ve7v8dk2

ve7v8dk21#

使用org.wildfly.core:wildfly-controller-client API,您可以执行以下操作:

try (ModelControllerClient client = ModelControllerClient.Factory.create("localhost", 9990)) {
    final ModelNode address = Operations.createAddress("system-property", "test.property");
    ModelNode op = Operations.createRemoveOperation(address);
    ModelNode result = client.execute(op);
    if (!Operations.isSuccessfulOutcome(result)) {
        throw new RuntimeException("Failed to remove property: " + Operations.getFailureDescription(result).asString());
    }
    op = Operations.createAddOperation(address);
    op.get("value").set("test-value");
    result = client.execute(op);
    if (!Operations.isSuccessfulOutcome(result)) {
        throw new RuntimeException("Failed to add property: " + Operations.getFailureDescription(result).asString());
    }
}

您也可以使用REST API,但是您需要有一种方法来执行摘要式身份验证。

Client client = null;
try {
    final JsonObject json = Json.createObjectBuilder()
            .add("address", Json.createArrayBuilder()
                    .add("system-property")
                    .add("test.property.2"))
            .add("operation", "add")
            .add("value", "test-value")
            .build();
    client = ClientBuilder.newClient();
    final Response response = client.target("http://localhost:9990/management/")
            .request()
            .header(HttpHeaders.AUTHORIZATION, "Digest <settings>")
            .post(Entity.json(json));
    System.out.println(response.getStatusInfo());
} finally {
    if (client != null) client.close();
}

相关问题