cancel
Showing results for 
Search instead for 
Did you mean: 

How can we customize/override DateAdapter of de.hybris.platform.webservicescommons.jaxb.adapters?

SumitM01
Explorer
0 Kudos
229

Hi All,

OOTB DateAdapter class from webservicescommons has UTC time zone hardcoded. We need to make it system default time, but tried multiple ways to override the Adapter but nothing. 

de.hybris.platform.webservicescommons.jaxb.adapters.DateAdapter 

Can some suggest if there is any way we can override DateAdapter? There is one custom TimeZoneDateAdapter, we followed that process also to define our customer Date Adapter, but it didn't work.

SumitM01_0-1739385484048.png

Updated screenshots :

SumitM01_1-1739446247030.pngSumitM01_2-1739446269600.png

Solution for 2211.29:

SumitM01_0-1739523508881.png

 

 

Thanks.

View Entire Topic
mansurarisoy
Contributor
0 Kudos

Assuming that you have an OCC extension (e.g., customocc) generated from the yocc template and requiring commercewebservices, you can define a custom DateAdapter by following these steps:

  • Create CustomDateAdapter that extends XmlAdapter<String, Date>
  • Implement the marshal() and unmarshal() methods
  • Add the following bean definition to resources/occ/v2/trainingocc/web/spring/customocc-web-spring.xml

 

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="customJaxbContextFactory" />
    <property name="targetMethod" value="setTypeAdapters" />
    <property name="arguments">
        <list value-type="java.lang.Class">
            <value>your.package.customocc.jaxb.adapters.CustomDateAdapter</value>
            <value>de.hybris.platform.webservicescommons.jaxb.adapters.StringMapAdapter</value>
            <value>de.hybris.platform.webservicescommons.jaxb.adapters.XSSStringAdapter</value>
        </list>
    </property>
</bean>

 

  • Build the code with ant all command
  • Start server

I tried this on my local environment and successfully customized the output for Date attributes. The only drawback of this solution is that if SAP adds a new adapter to the customJaxbContextFactory bean, you will also need to add it to your bean definition.

To prevent this issue, we should remove DateAdapter from the list and add our CustomAdapter. However, I couldn't find an easy way to do this. I was able to achieve it using the following bean definitions, but it does not seem as simple and clear as the previous definition.

<bean id="customJaxbContextFactoryTypeAdaptersList" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="customJaxbContextFactory"/>
    <property name="targetMethod" value="getTypeAdapters"/>
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="customJaxbContextFactoryTypeAdaptersList"/>
    <property name="targetMethod" value="remove"/>
    <property name="arguments">
        <list value-type="java.lang.Class">
            <value>de.hybris.platform.webservicescommons.jaxb.adapters.DateAdapter</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="customJaxbContextFactoryTypeAdaptersList"/>
    <property name="targetMethod" value="add"/>
    <property name="arguments">
        <list value-type="java.lang.Class">
            <value>your.package.customocc.jaxb.adapters.CustomDateAdapter</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="customJaxbContextFactory" />
    <property name="targetMethod" value="setTypeAdapters" />
    <property name="arguments">
        <list>
            <ref bean="customJaxbContextFactoryTypeAdaptersList"/>
        </list>
    </property>
</bean>

You can choose whichever approach you prefer—both work and are valid options.

 

 

 

SumitM01
Explorer
0 Kudos

Thanks for the solution. For us (CCV2 2211.29) second option didn't work at all. First option worked but I needed to override - customJaxbContextFactory of Commerce Web services jaxb-coverters-spring.xml.

SumitM01_0-1739523564063.png

 

<alias name="defaultProjectNameJaxbContextFactory" alias="customJaxbContextFactory"/>
<bean id="defaultProjectNameJaxbContextFactory" parent="jaxbContextFactory">
<property name="metadataSourceFactory" ref="customMetadataSourceFactory" />
<property name="typeAdapters">
<list>
<value>com.ProjectName.jaxb.adaptors.ProjectNameDateAdapter</value>
<value>de.hybris.platform.webservicescommons.jaxb.adapters.StringMapAdapter</value>
<value>de.hybris.platform.webservicescommons.jaxb.adapters.XSSStringAdapter</value>
</list>
</property>
</bean>
mansurarisoy
Contributor
0 Kudos
Yes, it might be some differences because of the version, I tried it on 2211.31
SumitM01
Explorer
0 Kudos
yes. Thanks for the solution though. Got the idea from your solution to try usual spring bean override through alias and it worked. Thanks a lot.