fixed typos and programlisting formatting.
This commit is contained in:
parent
9cb3fb7012
commit
c8b8cf45b4
|
|
@ -916,8 +916,8 @@ public class MyService {
|
||||||
It addresses <emphasis>one-way</emphasis> conversion from one type to another and is not limited to just converting Strings.
|
It addresses <emphasis>one-way</emphasis> conversion from one type to another and is not limited to just converting Strings.
|
||||||
As discussed in the previous section, a Spring Container can be configured to use this system to bind bean property values.
|
As discussed in the previous section, a Spring Container can be configured to use this system to bind bean property values.
|
||||||
In addition, the Spring Expression Language (SpEL) uses this system to coerce Expression values.
|
In addition, the Spring Expression Language (SpEL) uses this system to coerce Expression values.
|
||||||
For example, when SpEL needs to coerce a <classname>Short</classname> to a <classname>Long</classname> to fullfill an
|
For example, when SpEL needs to coerce a <classname>Short</classname> to a <classname>Long</classname> to fulfill an
|
||||||
<function>expression.setValue()</function> attempt, the core.convert system performs the coersion.
|
<function>expression.setValue()</function> attempt, the core.convert system performs the coercion.
|
||||||
</para>
|
</para>
|
||||||
<para>
|
<para>
|
||||||
Now consider the type conversion requirements of a typical UI environment such as a web or desktop application.
|
Now consider the type conversion requirements of a typical UI environment such as a web or desktop application.
|
||||||
|
|
@ -1467,7 +1467,7 @@ public class MyController {
|
||||||
There are scenarios, particularly in large message-oriented business applications, where data and object transformation is required.
|
There are scenarios, particularly in large message-oriented business applications, where data and object transformation is required.
|
||||||
For example, consider a complex Web Service where there is a separation between the data exchange model and the internal domain model used to structure business logic.
|
For example, consider a complex Web Service where there is a separation between the data exchange model and the internal domain model used to structure business logic.
|
||||||
In cases like this, a general-purpose data mapping facility can be useful for automating the mapping between these disparate models.
|
In cases like this, a general-purpose data mapping facility can be useful for automating the mapping between these disparate models.
|
||||||
Spring 3 introduces such a facility built on the <link linkend="expressions-intro">Spring Expression Language</link> (SpEl).
|
Spring 3 introduces such a facility built on the <link linkend="expressions-intro">Spring Expression Language</link> (SpEL).
|
||||||
This facility is described in this section.
|
This facility is described in this section.
|
||||||
</para>
|
</para>
|
||||||
<section id="mapping-Mapping-API">
|
<section id="mapping-Mapping-API">
|
||||||
|
|
@ -1523,12 +1523,10 @@ public class PersonDtoPersonMapper implements Mapper<PersonDto, Person> {
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
SpelMapper mapper = new SpelMapper();
|
SpelMapper mapper = new SpelMapper();
|
||||||
mapper.map(aSource, aTarget);
|
mapper.map(aSource, aTarget);]]></programlisting>
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
<para>
|
<para>
|
||||||
By default, SpelMapper will map the fields on the source and target that have the same names.
|
By default, SpelMapper will map the fields on the source and target that have the same names.
|
||||||
If the field types differ, the mapping system will attempt a type coersion using Spring 3's <link linkend="core.convert">type conversion system</link>.
|
If the field types differ, the mapping system will attempt a type conversion using Spring 3's <link linkend="core.convert">type conversion system</link>.
|
||||||
Nested bean properties are mapped recursively.
|
Nested bean properties are mapped recursively.
|
||||||
Any mapping failures will trigger a MappingException to be thrown.
|
Any mapping failures will trigger a MappingException to be thrown.
|
||||||
If there are multiple failures, they will be collected and returned in the MappingException thrown to the caller.
|
If there are multiple failures, they will be collected and returned in the MappingException thrown to the caller.
|
||||||
|
|
@ -1546,9 +1544,7 @@ public class CreateAccountDto {
|
||||||
private String street;
|
private String street;
|
||||||
private String zip;
|
private String zip;
|
||||||
}
|
}
|
||||||
}
|
}]]></programlisting>
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
<para>
|
<para>
|
||||||
And the following target object type:
|
And the following target object type:
|
||||||
</para>
|
</para>
|
||||||
|
|
@ -1564,9 +1560,7 @@ public class Account {
|
||||||
private String state;
|
private String state;
|
||||||
private String zip;
|
private String zip;
|
||||||
}
|
}
|
||||||
}
|
}]]></programlisting>
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
<para>
|
<para>
|
||||||
Used in the following test case:
|
Used in the following test case:
|
||||||
</para>
|
</para>
|
||||||
|
|
@ -1592,9 +1586,7 @@ public void testDefaultSpelMappingBehavior() {
|
||||||
assertEquals("35452", target.getAddress().getZip());
|
assertEquals("35452", target.getAddress().getZip());
|
||||||
assertNull(target.getAddress().getCity());
|
assertNull(target.getAddress().getCity());
|
||||||
assertNull(target.getAddress().getState());
|
assertNull(target.getAddress().getState());
|
||||||
}
|
}]]></programlisting>
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
<para>
|
<para>
|
||||||
In this example, the <literal>number</literal>, <literal>name</literal>, and <literal>address</literal> properties are automatically mapped since they are present on both the source and target objects.
|
In this example, the <literal>number</literal>, <literal>name</literal>, and <literal>address</literal> properties are automatically mapped since they are present on both the source and target objects.
|
||||||
The AccountDto's <literal>address</literal> property is a JavaBean, so its nested properties are also recursively mapped.
|
The AccountDto's <literal>address</literal> property is a JavaBean, so its nested properties are also recursively mapped.
|
||||||
|
|
@ -1611,10 +1603,7 @@ public void testDefaultSpelMappingBehavior() {
|
||||||
Since the two property names are not the same, default auto-mapping would never be performed.
|
Since the two property names are not the same, default auto-mapping would never be performed.
|
||||||
Handle a situation like this by explicitly registering a mapping rule:
|
Handle a situation like this by explicitly registering a mapping rule:
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java">mapper.addMapping("name", "fullName");</programlisting>
|
||||||
mapper.addMapping("name", "fullName");
|
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
<para>
|
<para>
|
||||||
In this example, the <literal>name</literal> field will be mapped to the <literal>fullName</literal> field when the mapper is executed.
|
In this example, the <literal>name</literal> field will be mapped to the <literal>fullName</literal> field when the mapper is executed.
|
||||||
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.
|
||||||
|
|
@ -1624,10 +1613,7 @@ mapper.addMapping("name", "fullName");
|
||||||
<para>
|
<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 must be defined explicitly by disabling the "auto mapping" feature:
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java">mapper.setAutoMappingEnabled(false);</programlisting>
|
||||||
mapper.setAutoMappingEnabled(false);
|
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
<section id="mapping.SpelMapper-CustomConverter">
|
<section id="mapping.SpelMapper-CustomConverter">
|
||||||
|
|
@ -1642,9 +1628,7 @@ mapper.addMapping("name", "fullName").setConverter() { new Converter<String, Str
|
||||||
// do transformation
|
// do transformation
|
||||||
// return transformed value
|
// return transformed value
|
||||||
}
|
}
|
||||||
});
|
});]]></programlisting>
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
</section>
|
</section>
|
||||||
<section id="mapper.SpelMapper-IgnoringFields">
|
<section id="mapper.SpelMapper-IgnoringFields">
|
||||||
<title>Ignoring Fields</title>
|
<title>Ignoring Fields</title>
|
||||||
|
|
@ -1652,15 +1636,12 @@ mapper.addMapping("name", "fullName").setConverter() { new Converter<String, Str
|
||||||
Sometimes you need to exclude a specific field on a source object from being mapped.
|
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 a mapping as excluded:
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java">mapper.addMapping("name").setExclude();</programlisting>
|
||||||
mapper.addMapping("name").setExclude();
|
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
</section>
|
</section>
|
||||||
<section id="mapper.SpelMapper-CustomTypeConverters">
|
<section id="mapper.SpelMapper-CustomTypeConverters">
|
||||||
<title>Registering Custom Type Converters</title>
|
<title>Registering Custom Type Converters</title>
|
||||||
<para>
|
<para>
|
||||||
You can also install Converters to coerse values of different types in a custom way.
|
You can also install Converters to convert values of different types in a custom way.
|
||||||
Do this by obtaining the mapper's ConverterRegistry:
|
Do this by obtaining the mapper's ConverterRegistry:
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
|
|
@ -1669,9 +1650,7 @@ mapper.getConverterRegistry().addConverter(new Converter<String, Date>() {
|
||||||
// do conversion
|
// do conversion
|
||||||
// return transformed value
|
// return transformed value
|
||||||
}
|
}
|
||||||
});
|
});]]></programlisting>
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
<para>
|
<para>
|
||||||
The example Converter above will be invoked anytime a String field is mapped to a Date field.
|
The example Converter above will be invoked anytime a String field is mapped to a Date field.
|
||||||
</para>
|
</para>
|
||||||
|
|
@ -1688,9 +1667,7 @@ mapper.addNestedMapper(new Mapper<AddressDto, Address>() {
|
||||||
// do target bean mapping here
|
// do target bean mapping here
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
});
|
});]]></programlisting>
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
<para>
|
<para>
|
||||||
The example above registers a nested Mapper that will map nested AddressDto properties to nested Address properties.
|
The example above registers a nested Mapper that will map nested AddressDto properties to nested Address properties.
|
||||||
This particular nested Mapper is "hand-coded", but it could have easily been another generic SpelMapper instance.
|
This particular nested Mapper is "hand-coded", but it could have easily been another generic SpelMapper instance.
|
||||||
|
|
@ -1716,11 +1693,9 @@ public interface MappableType<T> {
|
||||||
|
|
||||||
EvaluationContext getEvaluationContext(T object, ConversionService conversionService);
|
EvaluationContext getEvaluationContext(T object, ConversionService conversionService);
|
||||||
|
|
||||||
}
|
}]]> </programlisting>
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
<para>
|
<para>
|
||||||
To plugin your custom MappableType, inject a custom MappableTypeFactory into your SpelMapper:
|
To plug in your custom MappableType, inject a custom MappableTypeFactory into your SpelMapper:
|
||||||
</para>
|
</para>
|
||||||
<programlisting language="java"><![CDATA[
|
<programlisting language="java"><![CDATA[
|
||||||
SpelMapper mapper = new SpelMapper();
|
SpelMapper mapper = new SpelMapper();
|
||||||
|
|
@ -1728,9 +1703,7 @@ MappableTypeFactory factory = new MappableTypeFactory();
|
||||||
factory.add(new MyCustomMappableType());
|
factory.add(new MyCustomMappableType());
|
||||||
factory.add(new MapMappableType());
|
factory.add(new MapMappableType());
|
||||||
factory.add(new BeanMappableType());
|
factory.add(new BeanMappableType());
|
||||||
mapper.setMappableTypeFactory(factory);
|
mapper.setMappableTypeFactory(factory);]]></programlisting>
|
||||||
]]>
|
|
||||||
</programlisting>
|
|
||||||
<note>
|
<note>
|
||||||
<para>
|
<para>
|
||||||
The Spring team encourages you to contribute any generally useful MappableType extensions back to the community.
|
The Spring team encourages you to contribute any generally useful MappableType extensions back to the community.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue