繁体   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