Data Mapping Mechanism

The mapping data mechanism facilitates mapping of the data between the source and destination objects.

The data mapping mechanism facilitates mapping of the data between the source and destination objects. It can be used in Web Services for:

  • mapping of the data between the model objects and web services DTO or
  • mapping of the data between the commerce layer data object and web services DTO.

The core functionality is exposed by the DataMapper interface providing several methods for mapping between source and destination objects. It supports simple objects, lists, sets and generics. Additionally, you are able to specify which fields you want to populate without a nesting limit. In order to simplify the conversion between similar objects, the default implementation of this interface uses Orika - a popular Java Bean mapper framework.

The DefaultDataMapper extends Orika's ConfigurableMapper and adds some more features:

  • automatically registers all custom filters, converters and mappers from the Spring context.
  • provides XML configuration for additional field mappings.
  • uses FieldSetBuilder and GeneralFieldFilter to map only specified fields of destination object (field levels).

To use this mechanism in your web services extension you have to configure it in the Spring context. Some beans are already defined in the context of webservicescommons extension, however some have to be defined in the Web Services web context. The following example presents the configuration you can find in the/webservicescommons/resources/webservicescommons-spring.xml.

...    
	<!-- DTO MAPPING -->
				
	<bean id="abstractCustomMapper"
		 class="de.hybris.platform.webservicescommons.mapping.mappers.AbstractCustomMapper"
		 abstract="true">
	  <property name="fieldSelectionStrategy" ref="fieldSelectionStrategy"/>
	</bean>
				
	<alias alias="fieldSelectionStrategy" name="defaultFieldSelectionStrategy"/>
	<bean id="defaultFieldSelectionStrategy"
		class="de.hybris.platform.webservicescommons.mapping.impl.DefaultFieldSelectionStrategy"/>
				
	<alias alias="fieldSetLevelMapping" name="defaultFieldSetLevelMapping"/>
	<bean name="defaultFieldSetLevelMapping"
		class="de.hybris.platform.webservicescommons.mapping.config.FieldSetLevelMapping"
		abstract="true"/>
				
	<!-- Field Mappings -->
	<alias alias="fieldMapper" name="defaultFieldMapper"/>
	<bean name="defaultFieldMapper"
		class="de.hybris.platform.webservicescommons.mapping.config.FieldMapper"
		abstract="true"/>
...

The base bean (DefaultDataMapper) should be defined in the Web Services web context, because the DefaultDataMapper automatically registers all custom filters, converters and mappers from the Spring context and it has to be in the same context as these custom beans.

For an example of web context configuration see DTO Mapping and Response Configuration.