简体   繁体   English

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

[英]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)

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.萨蒂亚。

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 @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;
}

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

相关问题 两个类具有相同的 XML 类型名称 - Two Classes have same XML type name 两个类具有相同的XML类型名称“objectFactory” - Two classes have the same XML type name “objectFactory” IllegalAnnotationException:两个类具有相同的 XML 类型名称 - IllegalAnnotationException: Two classes have the same XML type name 使用两个类具有相同的 XML 类型名称 - Use two classes have the same XML type name jaxb2-maven-plugin 在生成 xsd 时忽略命名空间 - 两个类具有相同的 XML 类型 - jaxb2-maven-plugin ignores namespace when generating xsd - Two classes have the same XML type wsdl2java两个类具有相同的XML类型名称“{http://***.***。$。* * / * / / / / / *}} objectFactory” - wsdl2java Two classes have the same XML type name “{http://***.***.***.***/***/***/***}objectFactory” IllegalAnnotationExceptions:两个类具有相同的XML类型名称。 CXF插件生成的代码 - IllegalAnnotationExceptions: Two classes have the same XML type name. Code generated by CXF plugin java +消费SOAP Web服务+“两个类具有相同的XML类型名称……” - java + Consuming a SOAP web service + “Two classes Have the same XML type name …” 获取具有相同名称JAXB的多个XML元素 - Getting multiple XML elements with the same name JAXB 如何创建两个具有相同名称,签名和返回类型的方法的类,就像它们实现相同的接口一样 - How to make two classes which both have method with the same name, signature and return type behave like they would implement the same interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM