获取JAXB异常,例如“两个类具有相同的XML类型名称......”。 [英] Getting the JAXB exception like "Two classes have the same XML type name..."

查看:247
本文介绍了获取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:1
IllegalAnnotationExceptions计数两个类具有相同的XML
类型名称city。使用@ XmlType.name和@ XmlType.namespace
为它们分配
不同的名称。此问题与以下
位置有关:位于com.model.City的com.model.City,com.model.Address的
com.model.Address.getCurrentCity()此
问题与以下位置有关:at com.common.City
at public com.common.City com.model.Address.getPreviousCity()at
com.model.Address

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(未知
来源)
com.sun。 xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(未知
来源)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
S来自sun.reflect.DelegatingMethodAccessorImpl.invoke(未知
来源)的sun.reflect.NativeMethodAccessorImpl.invoke(未知
来源)sun.reflect.NativeMethodAccessorImpl.invoke0(原生
方法) at
javax.xml.bind.ContextFinder.newInstance(Unknown Source)at
javax.xml.bind.ContextFinder.find(Unknown Source)at java.lang.reflect.Method.invoke(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)

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)



我举了这样的例子:



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






另一个城市类 com.commonpackage。


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.

谢谢,

Satya。

推荐答案

注意:我是 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+User+Guide

这篇关于获取JAXB异常,例如“两个类具有相同的XML类型名称......”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆