如何配置Camel的jackson XmlMapper?

问题描述 投票:0回答:1

我有一个 SpringBoot 应用程序,使用 Camel 进行路由。 为了配置 jackson xmlMapper,我有以下 spring bean:

@Bean
public XmlMapper xmlMapper() {
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    return xmlMapper;
}

我希望 Camel 在解组收到的 POJO 时使用此映射器,如下所示:

from("direct:myRoute")
     .marshal().jacksonxml()
     .inOnly("activemq:outputQueue");

但这种方式不起作用:编组 xml 中存在空字段。 Camel 文档提供了使用 JacksonXMLDataFormat 的方法,如下所示:

JacksonXMLDataFormat format = new JacksonXMLDataFormat();
format.setInclude("NON_NULL");
from("direct:myRoute")
     .marshal(format)
     .inOnly("activemq:outputQueue");

它可以工作,但每次编组某些内容时我都必须指定 dataFormat,并且解组会变得更加棘手。

Camel的xmlMapper有统一的配置方式吗?

java spring-boot jackson apache-camel
1个回答
0
投票

你的解决方案应该是使用原型类型的bean

@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public org.apache.camel.component.jacksonxml.JacksonXMLDataFormat xmlMapperDataFormat(XmlMapper xmlMapper) {
    new JacksonXMLDataFormat(xmlMapper(), /*Your unmarshall class here */)
}

然后在你的路线中使用新的bean

from("direct:myRoute")
     .marshal(xmlMapperDataFormat)
     .inOnly("activemq:outputQueue");
© www.soinside.com 2019 - 2024. All rights reserved.