diff --git a/spring-framework-reference/src/validation.xml b/spring-framework-reference/src/validation.xml index 103194724c2..ee33f8934dc 100644 --- a/spring-framework-reference/src/validation.xml +++ b/spring-framework-reference/src/validation.xml @@ -1513,14 +1513,14 @@ public class PersonDtoPersonMapper implements Mapper<PersonDto, Person> { General-purpose SpelMapper Implementation A general purpose object-to-object mapping system exists in the org.springframework.mapping.support 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.
Usage - 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 MappingFactory.getDefaultMapper(). Then invoke the Mapper by calling its map(Object, Object) operation: Registering Explicit Mappings When default mapping rules are not sufficient, explicit mapping rules can be registered by obtaining a MapperBuilder and using it to construct a Mapper. - 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: mapper = Mapping between two fields with different names Suppose you need to map AccountDto.name to Account.fullName. - 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: + Since these two field names are not the same, the default auto-mapping rule would not apply. + Handle a requirement like this by explicitly registering a mapping rule: @@ -1626,7 +1626,7 @@ builder.addMapping("name", "fullName")]]>
- Mapping a single field value to multiple fields + Mapping a single field to multiple fields Suppose you need to map PersonDto.name to Person.firstName and Person.lastName. Handle a field-to-multi-field requirement like this by explicitly registering a mapping rule: @@ -1647,7 +1647,7 @@ builder.addMapping("name", new Mapper() {
- Mapping a single field value to multiple fields + Mapping multiple fields to a single field Suppose you need to map CreateAccountDto.activationDay and CreateAccountDto.activationTime to Account.activationDateTime. Handle a multi-field-to-field requirement like this by explicitly registering a mapping rule: @@ -1664,7 +1664,22 @@ builder.addMapping(new String[] { "activationDay", "activationTime" }, new Mappe In the example above, the activationDay and activationTime fields are mapped to the single activationDateTime field. - No default mapping is performed for activationDay and activationTime since an explicit mapping rule has been configured for these fields. + No default mapping is performed for activationDay or activationTime since an explicit mapping rule has been configured for these fields. + +
+
+ Mapping conditionally + + Suppose you need to map Map.countryCode to PhoneNumber.countryCode 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 countryCode field will only be mapped if the international field is 'true'. + international == 'true' is a boolean expression that must evaluate to true for the mapping to be executed. + No default mapping is performed for countryCode since an explicit mapping rule has been configured for this field.
@@ -1698,12 +1713,14 @@ builder.addMapping("name", "fullName").setConverter() { new Converter - builder.setExcludedFields("name"); + +
Registering Custom Type Converters - You may also install Converters to convert values of different types in a custom way: + You may also register custom Converters to convert values between mapped field of different types: () { @@ -1748,5 +1765,4 @@ builder.addNestedMapper(new Mapper() {
- \ No newline at end of file