Tutorial :Jaxb2Marshaller creating JAXBContext with empty namespace URI



Question:

Using Spring 3, I have created a MarshallingView, with the following marshaller:

<bean name="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r">      <property name="classesToBeBound">          <list>              <value>com.mydomain.xml.schema.Products</value>          </list>      </property>      <property name="marshallerProperties">          <map>              <entry key="com.sun.xml.bind.namespacePrefixMapper">                  <bean class="com.mydomain.xml.MyNamespacePrefixMapper"/>              </entry>          </map>      </property>  </bean>  

The MyNamespacePrefixMapper is supposed to map the schema of the Products object (XJC generated) to the default namespace, but it doesn't because the Jaxb2Marshaller is creating a JAXBContext that contains two different namespace URIs. One is my schema, the other one is a blank string. The blank string overrides any attempt by me to assign a default namespace.

Anyone know why this blank string is there or how I can get rid of it?


Solution:1

You could try using MOXy JAXB. The Spring configuration remains the same, you simply need to add a jaxb.properties file in with your model classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory  

See JAXB marshalling problem - probably namespace related . Instead of using a NamespacePrefixMapper you can simply configure the namesapce prefixes on the standard @XmlSchema annotation:

@javax.xml.bind.annotation.XmlSchema(       namespace = "http://www.example.org",       xmlns = {          @javax.xml.bind.annotation.XmlNs(prefix = "xsd", namespaceURI = "http://www.w3.org/2001/XMLSchema"),      },      elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)   package example;   

This produces XML like:

<?xml version="1.0" encoding="UTF-8"?>  <process xmlns="http://www.example.org" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »