A general purpose object-to-object mapping system exists in the <classname>org.springframework.mapping.support</classname> package.
Built on the flexible Spring Expression Language (SpEL), this system is capable of mapping between a variety of object types, including JavaBeans, Arrays, Collections, and Maps.
It can perform field-to-field, field-to-multi-field, and multi-field to field mappings.
It also can carry out type conversion and recursive mapping, often needed with rich object models.
Built on the Spring Expression Language (SpEL), this system is capable of mapping between a variety of object types, including JavaBeans, Arrays, Collections, and Maps.
It can perform field-to-field, field-to-multi-field, multi-field to field, and conditional mappings.
It also can carry out type conversion and recursive mapping, which are often required with rich object models.
</para>
<sectionid="mapping.SpelMapper-usage">
<title>Usage</title>
<para>
To obtain a general purpose object Mapper with its default configuration, simply call MappingFactory.getDefaultMapper().
To obtain a general purpose object Mapper with its default configuration, simply call <methodname>MappingFactory.getDefaultMapper()</methodname>.
Then invoke the Mapper by calling its <literal>map(Object, Object)</literal> operation:
</para>
<programlistinglanguage="java"><![CDATA[
@ -1599,7 +1599,7 @@ public void testDefaultSpelMappingBehavior() {
@@ -1599,7 +1599,7 @@ public void testDefaultSpelMappingBehavior() {
<title>Registering Explicit Mappings</title>
<para>
When default mapping rules are not sufficient, explicit mapping rules can be registered by obtaining a <classname>MapperBuilder</classname> and using it to construct a <classname>Mapper</classname>.
Explicit mapping rules always override the default.
Explicit mapping rules always override the default rule.
The MapperBuilder provides a fluent API for registering object-to-object Mapping rules:
<title>Mapping a single field value to multiple fields</title>
<title>Mapping multiple fields to a single field</title>
<para>
Suppose you need to map <literal>CreateAccountDto.activationDay</literal> and <literal>CreateAccountDto.activationTime</literal> to <literal>Account.activationDateTime</literal>.
Handle a multi-field-to-field requirement like this by explicitly registering a mapping rule:
In the example above, the <literal>activationDay</literal> and <literal>activationTime</literal> fields are mapped to the single <literal>activationDateTime</literal> field.
No default mapping is performed for <literal>activationDay</literal> and <literal>activationTime</literal> since an explicit mapping rule has been configured for these fields.
No default mapping is performed for <literal>activationDay</literal> or <literal>activationTime</literal> since an explicit mapping rule has been configured for these fields.
Suppose you need to map <literal>Map.countryCode</literal> to <literal>PhoneNumber.countryCode</literal> only if the source Map contains a international phone number.
Handle conditional mapping requirements like this by explicitly registering a mapping rule:
In the example above, the <literal>countryCode</literal> field will only be mapped if the international field is 'true'.
<literal>international == 'true'</literal> is a boolean expression that must evaluate to true for the mapping to be executed.
No default mapping is performed for <literal>countryCode</literal> since an explicit mapping rule has been configured for this field.
</para>
</section>
<sectionid="mapping.SpelMapper-Explicit-forcing">
@ -1698,12 +1713,14 @@ builder.addMapping("name", "fullName").setConverter() { new Converter<String, St
@@ -1698,12 +1713,14 @@ builder.addMapping("name", "fullName").setConverter() { new Converter<String, St
Sometimes you need to exclude a specific field on a source object from being mapped.
Do this by marking one or more source fields as excluded: