簡體   English   中英

如何將包裝器 JSON 添加到 Mutiny Multi?

[英]How to add wrapper JSON to Mutiny Multi?

我有一個 Java 方法,它使用流方法以 JSON 格式創建客戶信息,並在使用SmallRye MutinyMulti異步方法創建時返回。

我想將包裝器添加到使用Jackson JsonGenerator 我不明白如何添加相同的內容。 我相信我需要利用Multi.createBy().concatenating()來實現這一點

以下是我的方法:

public static Multi<Customer> generateCustomer(final Input input) {
    try {
        return Multi.createFrom().publisher(new CustomerPublisher(input));
    } catch (Exception e) {
        throw new NewException("Exception occurred during the generation of Customer : " + e);
    }
}

上面的方法目前異步返回類似這樣的東西:

[
  {
    "name":"Batman",
    "age":45,
    "city":"gotham"
  },
  {
    "name":"superman",
    "age":50,
    "city":"moon"
  }
]

我想為此 JSON 添加一個包裝器並創建如下內容:

{
  "isA": "customerDocument",
  "createdOn": "2022-10-10T12:29:43",
  "customerBody": {
    "customerList": [
      {
        "name": "Batman",
        "age": 45,
        "city": "gotham"
      },
      {
        "name": "superman",
        "age": 50,
        "city": "moon"
      }
    ]
  }
}

所以我添加了一個執行此操作的方法,我想調用相同的方法,但我無法將 append 與return Multi.createFrom().publisher相同

public class CustomerGenerator {

    private ByteArrayOutputStream jsonOutput;
    private JsonGenerator jsonGenerator;

    private CustomerGenerator() {
        try {
            jsonOutput = new ByteArrayOutputStream();
            jsonGenerator = new JsonFactory().createGenerator(jsonOutput).useDefaultPrettyPrinter();
        } catch (IOException ex) {
            throw new TestDataGeneratorException("Exception occurred during the generation of customer document : " + ex);
        }
    }

    public static Multi < Customer > generateCustomer(final Input input) {
        CustomerGenerator customerGenerator = new CustomerGenerator();
        customerGenerator.wrapperStart();
        try {
            return Multi.createFrom().publisher(new CustomerPublisher(input));
        } catch (Exception e) {
            throw new NewException("Exception occurred during the generation of Customer : " + e);
        } finally {
            System.out.println("ALL DONE");
            customerGenerator.wrapperEnd();
        }
    }

    public void wrapperStart() {
        try {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeStringField("type", "customerDocument");
            jsonGenerator.writeStringField("creationDate", Instant.now().toString());
            jsonGenerator.writeFieldName("customerBody");
            jsonGenerator.writeStartObject();
            jsonGenerator.writeFieldName("customerList");
        } catch (IOException ex) {
            throw new TestDataGeneratorException("Exception occurred during customer document wrapper creation : " + ex);
        }
    }

    public void wrapperEnd() {
        try {
            jsonGenerator.writeEndObject(); // End body
            jsonGenerator.writeEndObject(); // End whole json file
        } catch (IOException ex) {
            throw new TestDataGeneratorException("Exception occurred during customer document wrapper creation : " + ex);
        } finally {
            try {
                jsonGenerator.close();
                System.out.println("JSON DOCUMENT STRING : " + jsonOutput.toString());
            } catch (Exception e) {
                // do nothing
            }
        }
    }

}

當您包裝CustomGenerator控件之外的隨機輸入時,您的方法不會成功。

由於只有當所有發出的Customer object 都發出時,您才能生成最終的 output,並且假設您想保留面向 object 的方法(即不轉移到發出 JSON 令牌),您應該收集發出的客戶,然后包裝收集的項目進入你的最終 JSON output:

Uni<byte[]> result = Multi.createFrom().publisher(new CustomerPublisher(input))
    .collect()
    .asList()
    .map(customers -> {
        ByteArrayOutputStream jsonOutput = new ByteArrayOutputStream();
        JsonGenerator jsonGenerator = new JsonFactory().createGenerator(jsonOutput).useDefaultPrettyPrinter();
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("type", "customerDocument");
        jsonGenerator.writeStringField("creationDate", Instant.now().toString());
        jsonGenerator.writeFieldName("customerBody");
        jsonGenerator.writeStartObject();
        jsonGenerator.writeArrayFieldStart("customerList");
        customers.forEach(customer -> jsonGenerator.writeObject(customer));
        jsonGenerator.writeEndArray();
        jsonGenerator.writeEndObject();
        jsonGenerator.writeEndObject();
        jsonGnenerator.close();
        return jsonOutput.toByteArray();
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM