Java 获取 JAXB 异常,例如“两个类具有相同的 XML 类型名称...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19132789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:23:19  来源:igfitidea点击:

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

javaxmljaxb

提问by Satya

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

获取 JAXB 异常,如“两个类具有相同的 XML 类型名称...”,

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.namespaceto 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)

线程“main”com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException 中的异常:1 个 IllegalAnnotationExceptions 计数两个类具有相同的 XML 类型名称“city”。使用@XmlType.name 和@XmlType.namespace为它们分配不同的名称。此问题与以下位置有关:at com.model.City at public com.model.City com.model.Address.getCurrentCity() at com.model.Address 此问题与以下位置有关:at com.common .City at public com.common.City com.model.Address.getPreviousCity() at com.model.Address

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.

“com.common”包中的另一个城市类。

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.

我们需要创建 XSD 并需要对我们项目中的现有代码进行编组和解组(如上面的示例代码),代码没有任何注释,如“@XmlRootElement/@XmlType”,我们无法更改源代码。

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)?

我想知道是否有解决上述问题的解决方案或任何其他方法来创建 XSD 和编组/解组(如 MOXy..etc)?

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

如果我能从任何人那里得到解决方案就太好了....提前致谢。

Thanks,

谢谢,

Satya.

萨蒂亚。

采纳答案by bdoughan

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

注意:我是EclipseLink JAXB (MOXy) 的负责人和JAXB (JSR-222)专家组的成员。

If You Can Annotate the Class

如果您可以注释类

If you can modify the class you can simply add an @XmlTypeannotation to one of the Cityclasses to change the corresponding XML schema type name.

如果您可以修改该类,您只需@XmlType向其中一个City类添加注释即可更改相应的 XML 模式类型名称。

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.

MOXy 提供了一个外部映射文档扩展,可用于将 JAXB 元数据应用于无法更改的类。

<?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)?

1)我们只需要为一个 City 类编写绑定文件,还是需要编写所有其他 2 个类(我的意思是地址和另一个城市)?

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 Cityclasses then you don't need to include the others.

MOXy 的外部映射文档可用于扩充或完全替换(参见:http: //blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html)关于类的元数据。如果您需要进行的唯一更改是对其中一个City类进行更改,那么您不需要包含其他类。

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

2)在绑定文件中,您只考虑了类名,不需要采用 City 中定义的属性(我的意思是 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.

MOXy 与任何 JAXB 实现一样,将默认映射应用于所有类。您只需要为您希望映射行为与默认值不同的地方提供元数据。

3)We need to opt for MOXy for this?

3) 我们需要为此选择 MOXy 吗?

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.

JAXB 没有标准的外部映射文档。我所描述的一个是 MOXy 扩展。如果您使用的是 JAXB RI,您可以查看与 Annox 的集成。