[英]Getting the JAXB exception like "Two classes have the same XML type name..."
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".线程“main”com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException 中的异常:1 个 IllegalAnnotationExceptions 计数两个类具有相同的 XML 类型名称“city”。 Use @XmlType.name and @XmlType.namespace to assign different names to them.使用@XmlType.name 和@XmlType.namespace为它们分配不同的名称。 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.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
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.m 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.m ain(PojoToXSD.java:17) ain(PojoToXSD.java:17)
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.萨蒂亚。
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 modify the class you can simply add an @XmlType
annotation to one of the City
classes to change the corresponding XML schema type name.如果您可以修改该类,您只需将@XmlType
注释添加到City
类之一即可更改相应的 XML 模式类型名称。
package **com.common**;
@XmlType(name="city2")
public class City {
private String pinCode;
}
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想要查询更多的信息
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. MOXy 的外部映射文档可用于扩充或完全替换(参见: http : //blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html )类的元数据。 If the only change you need to make is to one of the City
classes then you don't need to include the others.如果您需要进行的唯一更改是对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. MOXy 与任何 JAXB 实现一样,将默认映射应用于所有类。 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? 3) 我们需要为此选择 MOXy 吗?
JAXB does not have a standard external mapping document. JAXB 没有标准的外部映射文档。 The one I have described is a MOXy extension.我所描述的一个是 MOXy 扩展。 If you are using the JAXB RI you could check out the integration with Annox.如果您使用的是 JAXB RI,您可以查看与 Annox 的集成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.