22

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.

Rahul
  • 44,383
  • 11
  • 84
  • 103
Satya
  • 273
  • 1
  • 3
  • 6

1 Answers1

33

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


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.

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.

Glenn Bech
  • 6,103
  • 4
  • 39
  • 56
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks for your reply. I have few queries here:1)we needs to write binding file for only one City class or required to write all other 2 classes(i mean Address and another City)? 2) In binding file you had considered only class name, not required to take properties defined in City(i mean pinCode)? 3)We need to opt for MOXy for this? – Satya Oct 02 '13 at 14:24
  • @user2837692 - I have updated my answer to address your follow up questions. – bdoughan Oct 02 '13 at 14:39
  • Once again thanks for your replies of my queries. I have very little knowledge on MOXy, I will try with binding files by going thru the docs. If any more help would required will post my queries for your assistance to resolve my issue!! :) – Satya Oct 02 '13 at 14:52
  • @user2837692 - The following example should help: https://github.com/bdoughan/blog20120418 – bdoughan Oct 02 '13 at 14:58
  • 2
    My issue got resolved: I have followed like-- Added a "@XmlSchema( namespace = "com.common", elementFormDefault = XmlNsForm.QUALIFIED)" to package-info.java – Satya Oct 03 '13 at 13:19
  • In my case, I used wsdl2java with autonameresolution flag, then specified a namespace to each class because I have two classes with same names in two different packages. – Mauricio Zárate Apr 09 '21 at 17:00