搜索

88.6. 映射扩展

download PDF

Dozer 组件将多个扩展实施到 Dozer 映射框架,作为自定义转换器。这些转换器实施直接由 Dozer 本身支持的映射功能。

88.6.1. 变量映射

通过变量映射,您可以将 Dozer 配置中的变量定义值映射到 target 字段,而不使用 source 字段的值。这等同于在其他映射框架中持续映射,您可以在其中为目标字段分配字面值。要使用变量映射,只需在映射配置中定义一个变量,然后将 VariableMapper 类映射到您选择的目标字段中:

<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
  <configuration>
    <variables>
      <variable name="CUST_ID">ACME-SALES</variable>
    </variables>
  </configuration>
  <mapping>
    <class-a>org.apache.camel.component.dozer.VariableMapper</class-a>
    <class-b>org.example.Order</class-b>
    <field custom-converter-id="_variableMapping" custom-converter-param="${CUST_ID}">
      <a>literal</a>
      <b>custId</b>
    </field>
  </mapping>
</mappings>

88.6.2. 自定义映射

通过自定义映射,您可以为源字段映射到 target 字段定义您自己的逻辑。它们的功能与 Dozer 客户转换器类似,有两个值得注意的区别:

  • 您可以在带有自定义映射的单一类中有多个转换器方法。
  • 不需要使用自定义映射实施特定于 Dozer 的接口。

使用映射配置中内置的 '_customMapping' converter 来声明自定义映射。此转换器的参数具有以下语法:

[class-name][,method-name]

方法名称是可选的 - Dozer 组件将搜索与映射所需的输入和输出类型匹配的方法。以下是自定义映射和配置示例。

public class CustomMapper {
    // All customer ids must be wrapped in "[ ]"
    public Object mapCustomer(String customerId) {
        return "[" + customerId + "]";
    }
}
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
  <mapping>
    <class-a>org.example.A</class-a>
    <class-b>org.example.B</class-b>
    <field custom-converter-id="_customMapping"
        custom-converter-param="org.example.CustomMapper,mapCustomer">
      <a>header.customerNum</a>
      <b>custId</b>
    </field>
  </mapping>
</mappings>

88.6.3. 表达式映射

表达式映射允许您使用 Camel 的强大 语言 功能来评估表达式,并将结果分配给映射中的目标字段。Camel 支持的任何语言都可以在表达式映射中使用。表达式的基本示例包括可以将 Camel 消息标头或交换属性映射到目标字段,或将多个源字段连接到目标字段中。映射表达式的语法为:

[language]:[expression]

将消息标头映射到目标字段的示例:

<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
  <mapping>
    <class-a>org.apache.camel.component.dozer.ExpressionMapper</class-a>
    <class-b>org.example.B</class-b>
    <field custom-converter-id="_expressionMapping" custom-converter-param="simple:\${header.customerNumber}">
      <a>expression</a>
      <b>custId</b>
    </field>
  </mapping>
</mappings>

请注意,表达式中的任何属性都必须使用 "\" 进行转义,以防止 Dozer 尝试解析使用 EL 定义的变量值时出现错误。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.