Getting the JAXB exception like “Two classes have the same XML type name…”

后端 未结 1 1701
灰色年华
灰色年华 2020-12-09 09:47

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

Here is the exception details:

Exception in thread \

相关标签:
1条回答
  • 2020-12-09 10:30

    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
    0 讨论(0)
提交回复
热议问题