获取 JAXB 异常,例如“两个类具有相同的 XML 类型名称..."
获取 JAXB 异常,例如两个类具有相同的 XML 类型名称...",
Getting the JAXB exception like "Two classes have the same XML type name...",
这里是异常详情:
线程main"中的异常com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:1IllegalAnnotationExceptions 的计数 两个类具有相同的 XML输入名称城市".使用@XmlType.name 和@XmlType.namespace 来分配给他们不同的名字.这个问题与以下有关地点:com.model.City 公共 com.model.Citycom.model.Address.getCurrentCity() 在 com.model.Address 这个问题与以下位置有关:在 com.common.City在公共 com.common.City com.model.Address.getPreviousCity() 在com.model.地址
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
在com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(未知来源)在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(未知来源)在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(未知来源)在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(未知来源)在com.sun.xml.internal.bind.v2.ContextFactory.createContext(未知来源)在com.sun.xml.internal.bind.v2.ContextFactory.createContext(未知源) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)在 java.lang.reflect.Method.invoke(未知源)在javax.xml.bind.ContextFinder.newInstance(未知来源)在javax.xml.bind.ContextFinder.find(未知来源)在javax.xml.bind.JAXBContext.newInstance(未知来源)在javax.xml.bind.JAXBContext.newInstance(未知来源)在com.PojoToXSD.main(PojoToXSD.java:17)
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)
我举了这样的例子:
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;
}
<小时>
com.common"包中的另一个城市类.
Another city class in "com.common" package.
package **com.common**;
public class City {
private String pinCode;
}
<小时>
我们需要创建 XSD 并且需要对我们项目中的现有代码进行编组和解组(如上面的示例代码),代码没有像@XmlRootElement/@XmlType"这样的任何注释,我们不能修改源代码.
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 和编组/解组(如 MOXy..etc)?
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.
谢谢,
萨提亚.
推荐答案
注意:我是EclipseLink JAXB (MOXy) 领导和 JAXB (JSR-222) 专家组.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
如果您可以修改类,您只需将 @XmlType
注释添加到 City
类之一即可更改相应的 XML 模式类型名称.
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;
}
如果你不能注释类
MOXy 提供了一个外部映射文档扩展,可用于将 JAXB 元数据应用于无法更改的类.
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>
更多信息
- http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
1) 我们只需要为一个 City 类或需要编写绑定文件写所有其他 2 个类(我的意思是地址和另一个城市)?
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 的外部映射文档可用于扩充或完全替换(参见:http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html) 类的元数据.如果您需要做的唯一更改是对 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) 在绑定文件中,您只考虑了类名,不需要获取 City 中定义的属性(我的意思是 pinCode)?
2) In binding file you had considered only class name, not required to take properties defined in City(i mean pinCode)?
像任何 JAXB 实现一样,MOXy 将默认映射应用于所有类.您只需提供您希望映射行为与默认值不同的地方的元数据.
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)我们需要为此选择 MOXy 吗?
3)We need to opt for MOXy for this?
JAXB 没有标准的外部映射文档.我所描述的是一个 MOXy 扩展.如果您使用的是 JAXB RI,您可以查看与 Annox 的集成.
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+用户+指南
相关文章