Search code examples
javajaxbtomcat7apache-axisxjc

JAXB Namespace translation in a AXIS2 WebService application on Tomcat 7


I've used xjc of jaxb 2.2.6 to generate a set of classes from a xsd file. By editing "package-info.java" I've associated to the different namespaces the prefix value. So I've created a Test Class with a main that Unmarshal an xml file, edit some information, and marshal the object in xml format. Everything works like a charm and javax.xml.bind.Marshaller object match correctly namespace and prefix as defined in package-info.

When I deploy this application as WS using axis2 on tomcat7 in the same machine and call a ws method that execute the code described above javax.xml.bind.Marshaller create an xml file with default namespace (ns1, ns2....).

The package-info.java that I've used is something like this:

@javax.xml.bind.annotation.XmlSchema(
        namespace = "....", 
        xmlns = {   
                @XmlNs(namespaceURI = "....", prefix = "myprefix"),
                @XmlNs(namespaceURI = "...", prefix = "myprefix2"),
            },
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ....;

import javax.xml.bind.annotation.XmlNs; 

This is a piece of right output (This output is obtained when I execute the code as "Java Application"):

.....
    <ContactInformation>
        <rm:ContactDescription>ASD</rm:ContactDescription>
        <rm:ContactRole>ASD</rm:ContactRole>
        <rm:ContactLocation/>
        <rm:AdditionalContactInformation>
            <xnl:PartyName>
                <xnl:PersonName>
                    <xnl:NameElement xnl:ElementType="FirstName">ASD</xnl:NameElement>
                    <xnl:NameElement xnl:ElementType="LastName">ASD</xnl:NameElement>
                </xnl:PersonName>
                <xnl:OrganisationName>
                    <xnl:NameElement>ASD</xnl:NameElement>
                </xnl:OrganisationName>
            </xnl:PartyName>
        </rm:AdditionalContactInformation>
    </ContactInformation>
......

This is a piece of wrong output (This output is obtained when I execute the code inside an "Axis2/Tomcat7 WS Application"):

.....
  <ContactInformation>
        <ns2:ContactDescription>ASD</ns2:ContactDescription>
        <ns2:ContactRole>ASD</ns2:ContactRole>
        <ns2:ContactLocation/>
        <ns2:AdditionalContactInformation>
            <ns7:PartyName>
                <ns7:PersonName>
                    <ns7:NameElement ns7:ElementType="FirstName">ASD</ns7:NameElement>
                    <ns7:NameElement ns7:ElementType="LastName">ASD</ns7:NameElement>
                </ns7:PersonName>
                <ns7:OrganisationName>
                    <ns7:NameElement>ASD</ns7:NameElement>
                </ns7:OrganisationName>
            </ns7:PartyName>
        </ns2:AdditionalContactInformation>
    </ContactInformation>
......

For each case exists a package-info.java where namespaces translation are declared.

How can I resolve this issue?


Solution

  • A JAXB (JSR-222) implementation is not required to use the prefixes as defined in the @XmlSchema annotation. The prefixes are used are not significant, and the namespace qualification between JAXB (JAX-WS) implementations will be the same although the prefixes may be different.