我有一个 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/