cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

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

SumitM01
Explorer
0 Likes
1,040

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.

Accepted Solutions (1)

Accepted Solutions (1)

mansurarisoy
Contributor
0 Likes

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 Likes

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 Likes
Yes, it might be some differences because of the version, I tried it on 2211.31
SumitM01
Explorer
0 Likes
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.

Answers (1)

Answers (1)

cieslo
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi,

I can see that you tried to follow the steps described on Help page: Custom Configuration of the HTTP Message Converters. However from the screenshot I can see that you still used the OOTB DateAdapter. Have you tried implementing your own adapter and adding it to the list instead of the OOTB one? Keep in mind that this change will affect only the webservice available in your custom extension. If you wish to implement this change globally you would need to use a mechanism to change OOTB files. A simple way to achieve that is to use buildcallbacks in your custom extension and use ant to replace the OOTB list of adapters with the custom one. You can check an example how to do it in this answer. Hope this helps.

SumitM01
Explorer
0 Likes
That screenshot is for reference only. But yes we tried to modify the list by adding our custom adapter (Updated screenshot in Post). But it didn't work, Can you please check once if anything missing? Else will try buildcallbacks.