@ -38,6 +38,10 @@ final class FieldToMultiFieldMapping implements SpelMapping {
@@ -38,6 +38,10 @@ final class FieldToMultiFieldMapping implements SpelMapping {
@ -52,7 +69,7 @@ final class MultiFieldToFieldMapping implements SpelMapping {
@@ -52,7 +69,7 @@ final class MultiFieldToFieldMapping implements SpelMapping {
@ -1599,7 +1599,8 @@ public void testDefaultSpelMappingBehavior() {
@@ -1599,7 +1599,8 @@ 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.
The MapperBuilder provides a fluent API for registering object-to-object Mapping rules:
<title>Mapping a single field value to multiple fields</title>
<para>
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:
</para>
<programlistinglanguage="java"><![CDATA[
builder.addMapping("name", new Mapper<String,Person>() {
public Person map(String name, Person person) {
String[] names = name.split(" ");
person.setFirstName(names[0]);
person.setLastName(names[1]);
return person;
}
});]]>
</programlisting>
<para>
In this example, the <literal>name</literal> field will be mapped to the <literal>fullName</literal> field when the mapper is executed.
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.
<title>Mapping a single field value to multiple fields</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:
</para>
<programlistinglanguage="java"><![CDATA[
builder.addMapping(new String[] { "activationDay", "activationTime" }, new Mapper<CreateAccountDto,AccountDto>() {
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.
No default mapping is performed for <literal>activationDay</literal> and <literal>activationTime</literal> since an explicit mapping rule has been configured for these fields.
</para>
</section>
<sectionid="mapping.SpelMapper-Explicit-forcing">
<title>Forcing Explicit Mappings</title>
<para>
You can require that all mapping rules must be defined explicitly by disabling the "auto mapping" feature:
You can require that all mapping rules be defined explicitly by disabling the "auto mapping" feature:
</para>
<programlistinglanguage="java"><![CDATA[
builder.setAutoMappingEnabled(false);]]>
@ -1651,14 +1696,14 @@ builder.addMapping("name", "fullName").setConverter() { new Converter<String, St
@@ -1651,14 +1696,14 @@ builder.addMapping("name", "fullName").setConverter() { new Converter<String, St
<title>Ignoring Fields</title>
<para>
Sometimes you need to exclude a specific field on a source object from being mapped.
Do this by marking a mapping as excluded:
Do this by marking one or more source fields as excluded: