How to add namespace attribute to XmlType Annotation in XJC generated Classes


I recently came across a really frustrating issue on a client site when trying to marshal a webservice response. The client code was generated using the XJC compiler in ANT and worked fine in local unit tests and when called on a JBoss application server running locally. The problem occurred when we tried to run the same code on a old application server that is used in production. We kept getting the following error:

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 219 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "". Use @XmlType.name and @XmlType.namespace to assign different names to them.
        this problem is related to the following location:

It looked like the other application server was not able to able to see the namespace for the generated classes that was generated in package-info.java. As a result there was a conflict with any class in the generated code with the same class name when the response was unmarshalled. We were using the standard Spring Jaxb2Marshaller so I know it could not be that:


	
		
			……
        	
	
 

I searched the web for information on how to add the namespace attribute to the generated classes but there were a lot of people looking for the same information and not answers:
How do I add a namespace attribute to an element in JAXB when marshalling?
Finally I found this page (http://www.java.net/node/655594) and the suggestion to use the “-npa” argument to the xjc compiler. This argument tells the compiler to suppress generation of package level annotations (**/package-info.java). You can see a full list of XJC commands here.

You can added it to the ant task as follows:

    

          
          
          	
          

This will tell the XJC compiler to add the namespace attribute to the @XMLType annotation:

@XmlType(name = "GeneratedClass", namespace = "http://www.mynamespace.com/API/V2", propOrder = {
	    "value"
})	

The marshaller is now able to map between the node in the response and the correct class.

, , ,

  1. #1 by Arpit Sharma on September 20, 2018 - 3:41 pm

    i am trying to generate java source out of xsds but facing the same issue. it is still not resolved.

  2. #2 by body Your paper on October 24, 2018 - 9:35 am

    A great way to be sure that your activity link is practical
    is always to reveal it to another person. You are likely around the right monitor if you explain what you consider the connection is plus they obtain it.
    Paul’s cathedral created Outdated Unhappinessis home|the property of
    Old Unhappiness was designed by Christopher Wren, who was
    the seventeenth-century designer A college training is still significant prerequisite
    for getting a decent career, plus a Master stage is
    still the road to raised management. The very best forecasts of task progress in the state
    suggest that the economy will continue to favor those with the best years of training.
    Paul’s cathedral|Christopher Wren, who had been the seventeenth century designer A baggage control applies for lighting aircraft transfers between camps.
    Gentle bags are encouraged for these routes. You will typically be encouraged of what’s needed (if any) at that time of booking.
    Paul’s cathedral designed Old Agony’s residence|the home of Old Agony
    was designed by Wren, who had been the seventeenth century architect Authentic companies – especially those based in great britain – must have a true handle, a tax number, along
    with a registered business number as possible look at Google Maps.
    Paul’s cathedral|Christopher Wren, who had been the seventeenth century builder 3 body sentences are
    usually consisted of by a 5 part composition. Each passage must consist of
    a powerful discussion or notion followed closely by encouraging detail factors.
    Paul’s cathedral designed the residence of Outdated Agony|Christopher Wren, who was A part that is
    good is really a number of paragraphs that is good around ONE main place.
    Paul’s cathedral designed the property of Outdated Agony|Christopher Wren, who had been Of
    Paul’s cathedral|Christopher Wren, who was the seventeenth-century architect A brief and
    distinct format could make your essay easyto create.
    Providing a great deal of service, creating a robust content emphasis, and introducing your
    ideas plainly produce the composition better to comprehend.
    The 5-section composition we will discuss helps you to
    coordinate your tips right into a written format.
    Your readeris knowledge and curiosity, and therefore your
    levels by organising the ideas of the report into this structure you’ll boost your dissertation construction. Paul’s cathedral|Wren, who was the seventeenth-century designer A great deal of
    people can not support but want to maintain their neighbors.

(will not be published)