A general purpose object-to-object mapping system exists in the <classname>org.springframework.mapping.support</classname> package.
A general purpose object-to-object mapping system exists in the <classname>org.springframework.mapping.support</classname> package.
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.
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 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.
It also can carry out type conversion and recursive mapping, which are often required with rich object models.
<title>Mapping a single field to multiple fields</title>
<title>Mapping a single field to multiple fields</title>
<para>
<para>
Suppose you need to map <literal>PersonDto.name</literal> to <literal>Person.firstName</literal> and <literal>Person.lastName</literal>.
Suppose you need to map <literal>PersonDto.name</literal> to <literal>Person.firstName</literal> and <literal>Person.lastName</literal>.
Handle a field-to-multi-field requirement like this by explicitly registering a mapping rule:
Handle a field-to-multi-field requirement like this by explicitly registering a mapping rule:
</para>
</para>
<programlistinglanguage="java"><![CDATA[
<programlistinglanguage="java"><![CDATA[
builder.addMapping("name", new Mapper<String,Person>() {
builder.addMapping("name", new Mapper<String,Person>() {
public Person map(String name, Person person) {
public Person map(String name, Person person) {
String[] names = name.split(" ");
String[] names = name.split(" ");
@ -1640,19 +1641,19 @@ builder.addMapping("name", new Mapper<String, Person>() {
return person;
return person;
}
}
});]]>
});]]>
</programlisting>
</programlisting>
<para>
<para>
In the example above, the first part of the <literal>name</literal> field will be mapped to the <literal>firstName</literal> field and the second part will be mapped to the <literal>lastName</literal> field.
In the example above, the first part of the <literal>name</literal> field will be mapped to the <literal>firstName</literal> field and the second part will be mapped to the <literal>lastName</literal> field.
No default mapping will be performed for <literal>name</literal> since an explicit mapping rule has been configured for this field.
No default mapping will be performed for <literal>name</literal> since an explicit mapping rule has been configured for this field.
<title>Mapping multiple fields to a single field</title>
<title>Mapping multiple fields to a single field</title>
<para>
<para>
Suppose you need to map <literal>CreateAccountDto.activationDay</literal> and <literal>CreateAccountDto.activationTime</literal> to <literal>Account.activationDateTime</literal>.
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:
Handle a multi-field-to-field requirement like this by explicitly registering a mapping rule:
</para>
</para>
<programlistinglanguage="java"><![CDATA[
<programlistinglanguage="java"><![CDATA[
builder.addMapping(new String[] { "activationDay", "activationTime" }, new Mapper<CreateAccountDto,AccountDto>() {
builder.addMapping(new String[] { "activationDay", "activationTime" }, new Mapper<CreateAccountDto,AccountDto>() {
public Account map(CreateAccountDto dto, Account account) {
public Account map(CreateAccountDto dto, Account account) {
In the example above, the <literal>activationDay</literal> and <literal>activationTime</literal> fields are mapped to the single <literal>activationDateTime</literal> field.
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> or <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.
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:
Handle conditional mapping requirements like this by explicitly registering a mapping rule: