If you want to add new properties to the existing DTO, if you want to extend DTO without touching the original xml definition, SAP Commerce Hybris offers a very easy way.
Add a new property to the DTO
I want to add additional properties to an OOTB (Out Of The Box) DTO. de.hybris.platform.commercewebservicescommons.dto.product.ProductWsDTO
I want to add a new property called isNew to ProductWsDTO.
Go to your webservices extension. It might be <name>webservices
Open <name>webservices-beans.xml file under resources folder.
Add the following lines.
<bean class="de.hybris.platform.commercewebservicescommons.dto.product.ProductWsDTO"> <property name="isNew" type="java.lang.Boolean"/> </bean>
Mapper Configuration
If you have same field in the ProductData you should set fieldMapper to auto mapping of this field.
Go to <name>webservices extension → web → webroot → v2 → dto-level-mappings-v2-spring.xml
Search for ProductWsDTO
if does not exist, create the below lines. If there is, just add your new field in the desired entry. Field mappers have 3 entry options; BASIC, DEFAULT and FULL. Add your new field as your requirement. I want to send this field for only detailed requests with FULL.
<bean parent="fieldSetLevelMapping" id="productWsDTOFieldSetLevelMapping"> <property name="dtoClass" value="de.hybris.platform.commercewebservicescommons.dto.product.ProductWsDTO"/> <property name="levelMapping"> <map> <entry key="BASIC" value=""/> <entry key="DEFAULT" value=""/> <entry key="FULL" value="isNew"/> </map> </property> </bean>
run ant clean all
You can find the new field in the class de.hybris.platform.commercewebservicescommons.dto.product.ProductWsDTO