gpt4 book ai didi

java - 获取像 "Two classes have the same XML type name..."这样的 JAXB 异常

转载 作者:IT老高 更新时间:2023-10-28 21:00:36 26 4
gpt4 key购买 nike

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

这里是异常详情:

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)

我举了这样的例子:

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”包中的另一个城市类。

package **com.common**;

public class City {

private String pinCode;
}

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

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

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

谢谢,

萨提亚。

最佳答案

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

如果你可以注释类

如果您可以修改类,您只需将 @XmlType 注释添加到 City 类之一即可更改相应的 XML 模式类型名称。

package **com.common**;

@XmlType(name="city2")
public class City {

private String pinCode;
}

如果你不能注释类

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>

更多信息


更新

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 类之一,那么您不需要包含其他类。

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

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

3)We need to opt for MOXy for this?

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

关于java - 获取像 "Two classes have the same XML type name..."这样的 JAXB 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19132789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com