更新Vert.x JSON配置文件时的HTTP请求

huangapple go评论68阅读模式
英文:

Update vertx Json config file during http request

问题

以下是您要翻译的内容:

Json Config:

{
  "REQUEST_OPTIONS": {
    "dataFilter": [
      {
        "name": "xxx",
        "values": [1, 2, 3] -> want to edit this specific field!
      },
      {
        "name": "yyy",
        "values": [4, 5, 6]
      }
    ]
  }
}

Start method:

@Override
public void start(Promise<Void> startPromise) {
    CompositeFuture.all(
            //Some other Futures ,
            Future.future(ConfigRetriever.create(vertx)::getConfig)
                    .flatMap(config -> storeData(config)
                            .compose(maybeStoreData -> CompositeFuture.all(
                                    matcherVerticle(subject).apply(maybeStoreData),
                                    http(sharedData, maybeStoreData).apply(config)
                            )))
            .<Void>mapEmpty()
            .onComplete(startPromise);
}

Router code:

router.get("/api/action").handler(performAction(configJson));

Action request:

private Handler<RoutingContext> performAction(JsonObject config) {
    return routingContext -> Try.of(() -> Json.decodeValue(routingContext.getBody(), RequestDto.class))
            .onFailure(ex -> log.warn("Failed to process the RequestDto: ", ex))
            .andThen(requestDto -> ???UPDATE CONFIG FILE according to requestDto ???)
            .onFailure(ex -> log.warn("Failed to save request: ", ex))
            .onFailure(ex -> routingContext.response().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()))
            .onSuccess(handler -> routingContext.response().setStatusCode(HttpResponseStatus.OK.code()));
}
英文:

I have VertX application with a config.json file. I want to edit one of the properties in the file during an HTTP request.

Is it possible?

Json Config:

{
  &quot;REQUEST_OPTIONS&quot;: {
    &quot;dataFilter&quot;: [
      {
        &quot;name&quot;: &quot;xxx&quot;,
        &quot;values&quot;: [1, 2, 3] -&gt; want to edit this specific field!
      },
      {
        &quot;name&quot;: &quot;yyy&quot;,
        &quot;values&quot;: [4, 5, 6]
      }
    ]
  }
}

Start method:

@Override
public void start(Promise&lt;Void&gt; startPromise) {
    CompositeFuture.all(
            //Some other Futures ,
            Future.future(ConfigRetriever.create(vertx)::getConfig)
                    .flatMap(config -&gt; storeData(config)
                            .compose(maybeStoreData -&gt; CompositeFuture.all(
                                    matcherVerticle(subject).apply(maybeStoreData),
                                    http(sharedData, maybeStoreData).apply(config)
                            ))))
            .&lt;Void&gt;mapEmpty()
            .onComplete(startPromise);

Router code:

router.get(&quot;/api/action&quot;).handler(performAction(configJson));

Action request:

private Handler&lt;RoutingContext&gt; performAction(JsonObject config) {
    return routingContext -&gt; Try.of(() -&gt; Json.decodeValue(routingContext.getBody(), RequestDto.class))
            .onFailure(ex -&gt; log.warn(&quot;Failed to process the RequestDto: &quot;, ex))
            .andThen(requestDto -&gt; ???UPDATE CONFIG FILE according to requestDto ???)
            .onFailure(ex -&gt; log.warn(&quot;Failed to save request: &quot;, ex))
            .onFailure(ex -&gt; routingContext.response().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()))
            .onSuccess(handler -&gt; routingContext.response().setStatusCode(HttpResponseStatus.OK.code()));
}

答案1

得分: 0

鉴于您已经有了您的JsonObject,您可以使用fileSystem().writeFile()将其写入磁盘。

这里是一个完整的示例:

Vertx vertx = Vertx.vertx();

var json = new JsonObject("{\n" +
        "  \"REQUEST_OPTIONS\": {\n" +
        "    \"dataFilter\": [\n" +
        "      {\n" +
        "        \"name\": \"xxx\",\n" +
        "        \"values\": [1, 2, 3]" +
        "      },\n" +
        "      {\n" +
        "        \"name\": \"yyy\",\n" +
        "        \"values\": [4, 5, 6]\n" +
        "      }\n" +
        "    ]\n" +
        "  }\n" +
        "}");

json.copy()
    .getJsonObject("REQUEST_OPTIONS")
    // 访问 dataFilter 数组
    .getJsonArray("dataFilter")
    // 获取 dataFilter 数组的第一个元素
    .getJsonObject(0)
    // 访问 values 数组
    .getJsonArray("values")
    .add(4);

vertx.fileSystem().writeFile("./new.json", json.toBuffer());
英文:

Given that you have your JsonObject already, you can write it to disk using fileSystem().writeFile().

Here's a full example:

Vertx vertx = Vertx.vertx();

var json = new JsonObject(&quot;{\n&quot; +
        &quot;  \&quot;REQUEST_OPTIONS\&quot;: {\n&quot; +
        &quot;    \&quot;dataFilter\&quot;: [\n&quot; +
        &quot;      {\n&quot; +
        &quot;        \&quot;name\&quot;: \&quot;xxx\&quot;,\n&quot; +
        &quot;        \&quot;values\&quot;: [1, 2, 3]&quot; +
        &quot;      },\n&quot; +
        &quot;      {\n&quot; +
        &quot;        \&quot;name\&quot;: \&quot;yyy\&quot;,\n&quot; +
        &quot;        \&quot;values\&quot;: [4, 5, 6]\n&quot; +
        &quot;      }\n&quot; +
        &quot;    ]\n&quot; +
        &quot;  }\n&quot; +
        &quot;}&quot;);

json.copy()
    .getJsonObject(&quot;REQUEST_OPTIONS&quot;)
    // Accessing the dataFilter array
    .getJsonArray(&quot;dataFilter&quot;)
    // Getting the first element of dataFilter array
    .getJsonObject(0)
    // Accessing the values array
    .getJsonArray(&quot;values&quot;)
    .add(4);

vertx.fileSystem().writeFile(&quot;./new.json&quot;, json.toBuffer());

huangapple
  • 本文由 发表于 2023年2月8日 17:44:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定