Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the JAXB exception like "Two classes have the same XML type name..."

Tags:

java

xml

jaxb

Getting the JAXB exception like "Two classes have the same XML type name...",

Here is the exception details:

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions Two classes have the same XML type name "city". Use @XmlType.name and @XmlType.namespace to assign different names to them. this problem is related to the following location: at com.model.City at public com.model.City com.model.Address.getCurrentCity() at com.model.Address this problem is related to the following location: at com.common.City at public com.common.City com.model.Address.getPreviousCity() at com.model.Address

at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source) at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source) at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at javax.xml.bind.ContextFinder.newInstance(Unknown Source) at javax.xml.bind.ContextFinder.find(Unknown Source) at javax.xml.bind.JAXBContext.newInstance(Unknown Source) at javax.xml.bind.JAXBContext.newInstance(Unknown Source) at com.PojoToXSD.main(PojoToXSD.java:17)

I took the example like:

package **com.model**; ---->this package contains 'Address' class and 'City' class

public class Address {

    private String areaName;
    private City currentCity;
    private com.common.City previousCity;
}

package com.model;

public class City {

    private String cityName;
}

Another city class in "com.common" package.

package **com.common**;

public class City {

    private String pinCode;
}

We need to create XSDs and needs to do the Marshalling and unmarshalling with the existing code in our project(like as above example code), code does not have any annotations like "@XmlRootElement/@XmlType" and we can not able to change the source code.

I would like to know is there any solution to fix the above issue or any other ways to create XSDs and marshaling/unmarshalling(like MOXy..etc)?

It would be great if i can get the solution from any one....May thanks in advance.

Thanks,

Satya.

like image 825
Satya Avatar asked Oct 02 '13 08:10

Satya


1 Answers

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

If You Can Annotate the Class

If you can modify the class you can simply add an @XmlType annotation to one of the City classes to change the corresponding XML schema type name.

package **com.common**;

@XmlType(name="city2")
public class City {

    private String pinCode;
}

If You Cannot Annotate the Class

MOXy offers an external mapping document extension that can be used to apply JAXB metadata to a class that cannot be changed.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="**com.common**">
    <java-types>
        <java-type name="City">
            <xml-type name="city2"/>
        </java-type>
    </java-types>
</xml-bindings>

For More Information

  • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html

UPDATE

1) we need to write binding file for only one City class or required to write all other 2 classes(i mean Address and another City)?

MOXy's external mapping document can used to augment or completely replace (see: http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html) the metadata on a class. If the only change you need to make is to one of the City classes then you don't need to include the others.

2) In binding file you had considered only class name, not required to take properties defined in City(i mean pinCode)?

MOXy like any JAXB implementation applies a default mapping to all classes. You only need to provide metadata for where you want the mapping behaviour to differ from the default.

  • http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html

3)We need to opt for MOXy for this?

JAXB does not have a standard external mapping document. The one I have described is a MOXy extension. If you are using the JAXB RI you could check out the integration with Annox.

  • http://confluence.highsource.org/display/ANX/JAXB+User+Guide
like image 165
bdoughan Avatar answered Oct 23 '22 23:10

bdoughan