JAXB - Preserving Namespace Prefixes in CXF WebServices - Texient - Learn 'n Share

Texient - Learn 'n Share

News & Articles on Technology, Finance & Marketing


Home Top Ad

Tuesday, 6 October 2015

JAXB - Preserving Namespace Prefixes in CXF WebServices

Follow the below steps if you want to preserve the namespace prefix values consistent.

First define a namespace prefix map, say JAXBNamespacePrefixesMap as below:

<util:map id="JAXBNamespacePrefixesMap">
   <entry key="http://www.texient.com/mail" value="ns1" />
   <entry key="http://www.texient.com/account" value="ns2" />
   <entry key="http://www.texient.com/contact" value="ns3" />
   <entry key="http://www.texient.com/address" value="ns4" />
   <entry key="http://www.texient.com/category" value="ns5" />

Now set this map as namespacePrefixes in JAXBElementProvider.

<bean id="jaxbProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
   <property name="jaxbElementClassMap" ref="JAXBElementClassMap" />
   <property name="marshallAsJaxbElement" value="true" />
   <property name="unmarshallAsJaxbElement" value="true" />
   <property name="collectionWrapperMap" ref="JAXBElementClassMap" />
   <property name="namespacePrefixes" ref="JAXBNamespacePrefixesMap" />

In the server configuration you will have jaxbProvider as a provider.

<jaxrs:server id="CXFService" address="/" >
      <entry key="json" value="application/json" />
      <entry key="xml" value="application/xml" />
      <ref bean="CXFServiceBean"/>
         <ref bean="jaxbProvider" />
         <ref bean="jsonpProvider" />            
       <ref bean="jsonpInInterceptor"/>
        <ref bean="jsonpOutInterceptor"/>

That's it! You will see the namespace prefix as defined in the mapping.

<ns1:person xmlns:ns1="http://www.texient.com/mail" xmlns:ns2="http://www.texient.com/account" xmlns:ns3="http://www.texient.com/contact" xmlns:ns4="http://www.texient.com/address" xmlns:ns5="http://www.texient.com/category">
      <ns4:address>Your address</ns4:address>

No comments:

Post a Comment

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to our feed and get articles like this delivered automatically to your feed reader? Like our Facebook Page.