Skip to main content Skip to complementary content

Configuring JAXB

CXF allows you to configure JAXB in two ways.

JAXB Properties

JAXB allows the application to specify two sets of properties that modify its behavior: context properties and marshaller properties. CXF allows applications to add to these properties. Take care. In some cases, CXF sets these properties for its own use.

You can add items to both of these property sets via the JAXBDataBinding class. The 'contextProperties' and 'marshallerProperties' properties (in the Spring sense) of JAXBDataBinding each store a Map<String, Object>. Whatever you put in the map, CXF will pass along to JAXB. See the JAXB documentation for details.

Example of Configuring a Context Property

<jaxws:server id="bookServer"
            serviceClass="org.apache.cxf.mytype.AnonymousComplexTypeImpl"
            address="http://localhost:8080/act" 
            bus="cxf">
            <jaxws:invoker>
            <bean class="org.apache.cxf.service.invoker.BeanInvoker">
            <constructor-arg>
            <bean class="org.apache.cxf.mytype.AnonymousComplexTypeImpl"/>
            </constructor-arg>
            </bean>
            </jaxws:invoker>
            <jaxws:dataBinding>
            <bean class="org.apache.cxf.jaxb.JAXBDataBinding">
            <property name="contextProperties">
            <map>
            <entry>
            <key>
            <value>com.sun.xml.bind.defaultNamespaceRemap</value>
            </key>
            <value>uri:ultima:thule</value>
            </entry>
            </map>
            </property>
            </bean>
            </jaxws:dataBinding>
            </jaxws:server>

Activating JAXB Validation of SOAP requests and responses

For the client side

<jaxws:client name="{http://apache.org/hello_world_soap_http}SoapPort"
            createdFromAPI="true">
            <jaxws:properties>
            <entry key="schema-validation-enabled" value="true" />
            </jaxws:properties>
            </jaxws:client>

You may also do this programmatically:

((BindingProvider)port).getRequestContext().put(
            "schema-validation-enabled", "true");

For the server side

<jaxws:endpoint name="{http://apache.org/hello_world_soap_http}SoapPort"
            wsdlLocation="wsdl/hello_world.wsdl" createdFromAPI="true">
            <jaxws:properties>
            <entry key="schema-validation-enabled" value="true" />
            </jaxws:properties>
            </jaxws:endpoint>

You can also use the org.apache.cxf.annotations.SchemaValidation annotation.

Namespace Prefix Management

The JAXB reference implementation allows the application to provide an object that in turn maps namespace URI's to prefixes. You can create such an object and supply it via the marshaller properties. However, CXF provides an easier process. The namespaceMap property of the JAXBDataBinding accepts a Map<String, String>. Think of it as a map from namespace URI to namespace prefix. If you load up this map, CXF will set up the necessary marshaller property for you.

Example of Configuring a Namespace Mapping

<jaxws:server id="bookServer"
            serviceClass="org.apache.cxf.mytype.AnonymousComplexTypeImpl"
            address="http://localhost:8080/act" 
            bus="cxf">
            <jaxws:invoker>
            <bean class="org.apache.cxf.service.invoker.BeanInvoker">
            <constructor-arg>
            <bean class="org.apache.cxf.mytype.AnonymousComplexTypeImpl"/>
            </constructor-arg>
            </bean>
            </jaxws:invoker>
            <jaxws:dataBinding>
            <bean class="org.apache.cxf.jaxb.JAXBDataBinding">
            <property name="namespaceMap">
            <map>
            <entry>
            <key>
            <value>
            http://cxf.apache.org/anonymous_complex_type/
            </value>
            </key>
            <value>BeepBeep</value>
            </entry>
            </map>
            </property>
            </bean>
            </jaxws:dataBinding>
            </jaxws:server>

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!