47.2. Jacksonxml 选项
JacksonXML dataformat 支持 15 个选项,如下所列。
| Name | 默认值 | Java 类型 | 描述 |
|---|---|---|---|
| xmlMapper |
| 查找并使用给定 id 的现有 XmlMapper。 | |
| prettyPrint | false |
| 要启用用户化的打印输出,请执行以下操作:默认为 false。 |
| unmarshalType |
| 当 unmarshalling 时使用的 java 类型的类名称。 | |
| jsonView |
| 当 marshalling a POJO to JSON 时,您可能想要从 JSON 输出中排除某些字段。通过 Jackson,您可以使用 JSON 视图来实现此目的。此选项是引用具有 JsonView 注释的类。 | |
| Include |
| 如果您想 marshal a pojo to JSON,并且 pojo 具有一些带有 null 值的字段。如果您想要跳过这些 null 值,您可以将这个选项设置为 NON_NULL。 | |
| allowJmsType |
| 用于 JMS 用户,以允许 JMS spec 中的 JMSType 标头指定一个 FQN 类名称来用于 unmarshal。 | |
| collectionType |
| 引用要使用的自定义集合类型,以便在 registry 中查找。这个选项应该很少被使用,但允许使用与基于 java.util.Collection 不同的集合类型。 | |
| useList |
| To unmarshal 到 Map 列表或 Pojo 的列表。 | |
| enableJaxbAnnotationModule |
| 在使用 jackson 时,是否启用 JAXB 注释模块。启用之后,Jackson 可以使用 JAXB 注释。 | |
| moduleClassNames |
| 使用自定义 Jackson 模块 com.fasterxml.jackson.databind.Module 指定为 String with FQN 类名称。可以使用逗号分隔多个类。 | |
| moduleRefs |
| 使用 Camel registry 中引用的自定义 Jackson 模块。可以使用逗号分隔多个模块。 | |
| enableFeatures |
| 在 Jackson com.fasterxml.jackson.databind.ObjectMapper 上启用的功能集合。这个功能应该是与 com.fasterxml.jackson.databind.SerializationFeature, com.fasterxml.jackson.databind.DeserializationFeature, 或 com.fasterxml.jackson.databind.MapperFeature multiple features 分开的名称。 | |
| disableFeatures |
| 在 Jackson com.fasterxml.jackson.databind.ObjectMapper 上禁用的功能集合。这个功能应该是与 com.fasterxml.jackson.databind.SerializationFeature, com.fasterxml.jackson.databind.DeserializationFeature, 或 com.fasterxml.jackson.databind.MapperFeature multiple features 分开的名称。 | |
| allowUnmarshallType |
| 如果启用,则允许 Jackson 在 unmarshalling 期间尝试使用 CamelJacksonUnmarshalType 标头。这只有在需要使用时才启用。 | |
| contentTypeHeader |
| 数据格式是否应该使用数据格式的类型设置 Content-Type 标头。例如,用于数据格式到 XML 的 application/xml 或用于数据格式的 application/json 发送到 JSON。 |
47.2.1. 在 Spring DSL 中使用 Jackson XML 复制链接链接已复制到粘贴板!
在 Spring DSL 中使用 Data Format 时,您需要首先声明数据格式。这在 DataFormats XML 标签中完成。
<dataFormats>
<!-- here we define a Xml data format with the id jack and that it should use the TestPojo as the class type when
doing unmarshal. The unmarshalType is optional, if not provided Camel will use a Map as the type -->
<jacksonxml id="jack" unmarshalType="org.apache.camel.component.jacksonxml.TestPojo"/>
</dataFormats>
<dataFormats>
<!-- here we define a Xml data format with the id jack and that it should use the TestPojo as the class type when
doing unmarshal. The unmarshalType is optional, if not provided Camel will use a Map as the type -->
<jacksonxml id="jack" unmarshalType="org.apache.camel.component.jacksonxml.TestPojo"/>
</dataFormats>
然后您可以在路由中引用此 id:
<route>
<from uri="direct:back"/>
<unmarshal><custom ref="jack"/></unmarshal>
<to uri="mock:reverse"/>
</route>
<route>
<from uri="direct:back"/>
<unmarshal><custom ref="jack"/></unmarshal>
<to uri="mock:reverse"/>
</route>
47.2.2. 从 marshalling 中排除 POJO 字段 复制链接链接已复制到粘贴板!
当 marshalling a POJO to XML 时,您可能想要从 XML 输出中排除某些字段。通过 Jackson,您可以使用 JSON 视图 来实现此目的。首先创建一个或多个标记类。
使用带 @JsonView 注释的标记类来包含/排除某些字段。该注释也适用于 getters。
最后,使用 Camel JacksonXMLDataFormat 将以上 POJO 放入 XML。
请注意,生成的 XML 中缺少 weight 字段:
<pojo age="30" weight="70"/>
<pojo age="30" weight="70"/>