簡體   English   中英

Apache Camel - 在 Rest DSL 中編組到 Json

[英]Apache Camel - Marshalling within Rest DSL to Json

我正在嘗試使用 Camel 中的 Rest DSL 處理 csv 文件。

當我將 CSV 和 Marshall 拆分為 JSON 時,出現了一些奇怪的行為。 這是我的代碼:

@Component
public class
ProcessHandler extends RouteBuilder {

    @Override
    protected void defineRoute() throws Exception {

        DataFormat csv = new BindyCsvDataFormat(CsvModel.class);

        rest("/")
                .post().produces("application/json")
                .route()

                .unmarshal(csv)
                .split(body()).parallelProcessing().streaming()
                .marshal().json(JsonLibrary.Gson)
                .filter().jsonpath("$[?(@.counter==3)]")
                .log("${body}")

但是我收到錯誤消息: Error during type conversion from type: java.lang.String to the required type: byte[] with value [CsvModel(....

但是,如果我按如下方式編輯路線:

@Component
public class
ProcessHandler extends RouteBuilder {

    @Override
    protected void defineRoute() throws Exception {

        DataFormat csv = new BindyCsvDataFormat(CsvModel.class);

        rest("/")
                .post().produces("application/json")
                .route()

                .unmarshal(csv)
                .marshal().json(JsonLibrary.Gson)
                .split(body()).parallelProcessing().streaming()
                //.filter().jsonpath("$[?(@.counter==3)]")
                .log("${body}")

它工作正常。

但是,我顯然無法正確處理我的消息,因為它被編組為字節表示。 如果不使用 Rest DSL,該路由也可以正常工作,所以我假設問題出在 http 響應上。 如果我嘗試以下操作:

@Component
public class
ProcessHandler extends RouteBuilder {

    @Override
    protected void defineRoute() throws Exception {

        DataFormat csv = new BindyCsvDataFormat(CsvModel.class);

        rest("/")
                .post().produces("application/json")
                .route()

                .unmarshal(csv)
                .marshal().json(JsonLibrary.Gson)

                .split(body()).parallelProcessing().streaming()
                .unmarshal().json(JsonLibrary.Gson)
                .filter().jsonpath("$[?(@.counter==3)]")
                .marshal().json(JsonLibrary.Gson)
                .log("${body}")

我犯了同樣的錯誤。 是否可以規范化格式,進行一些處理步驟,然后返回一個 Json? 我是不是在某個地方出錯了 - 我很想了解為什么會發生這種情況?

如果您在內部過濾而不是使用 JsonPath 來實現聚合策略,則可能更容易理解。

實際上,默認情況下使用split()方法不會給出您期望的結果

這是一個例子:

@Component
public class ProcessHandler extends RouteBuilder {

            @Override
            protected void defineRoute() throws Exception {

            DataFormat csv = new BindyCsvDataFormat(CsvModel.class);

            rest("/")
                    .post().produces("application/json")
                         .route()
                              .unmarshal(csv)
                              .split().method(ItemsSplittingStrategy.class, "splitItems")
                                  .parallelProcessing()
                                  .marshal().json(JsonLibrary.Gson)
                              .end()
                    .to("file:/file.json");
    }
}

在您的ItemsSplittingStrategy類中,您是否進行過濾。 你可以在這里找到一個簡單、明確的例子

我還邀請您檢查可用於拆分器和聚合器及其組合的所有功能。

暫無
暫無

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

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