java - 在 apache Camel 中解码后添加处理器

标签 java apache-camel gson marshalling

我有一个 Camel 路线设置,如下所示。
stage 文件夹中,我可以看到消息已正确编码到 json 中。
但是在这一行上进行解码时 body.setA("modified A");
我得到一个空指针异常,基本上主体为空。为什么

from("direct:stage").marshal().json(JsonLibrary.Gson)
.to("file://stage");

from("file://stage").unmarshal().json(JsonLibrary.Gson)
.process(new Processor() {                  
    @Override
    public void process(Exchange exchange) throws Exception {
        MyTest body = exchange.getIn().getBody(MyTest.class);           
        body.setA("modified A");
    }
}).to("direct:b");

最佳答案

您的正文很可能为空的原因是由于解码调用的配置。为了解码 json 对象,您需要提供字符串需要解码到的类。我不确定 100% 如何在 DSL 中做到这一点,但我之前已经这样做过,如下所示:

//In my spring context

<dataFormats>
    <json id="jack" library="Jackson" unmarshalTypeName="com.fun.model.TestModel"/>
</dataFormats>

//in my routeBuilder
from("")
    .unmarshal("jack")
    .to("other stuff");

关于java - 在 apache Camel 中解码后添加处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33058563/

相关文章:

java - JSON查询过滤传输

java - Gson 忽略值 = null 的 map 条目

java - Eclipse 未部署引用的库

java - 如何在Java中过滤掉非法的XML字符

java - Autowiring 类方法的空指针异常

apache-camel - Camel 中的 Http 连接池

java - 如何使用Gson解析以下json?

java - Spring 数据休息 : Foreign key is update with null after post call in one to many relationship

java - 家庭作业帮助,Java Pair 类(class)

java - 如何在 apache Camel 中使用 Splitter EIP 将 csv 文件一次拆分为 20 行?