[英]How to use inheritance in jaxb?
I have an abstract class address, and two classes that inherit from class address.. 我有一个抽象的类地址,以及两个继承自类地址的类。
classA and classB. classA和classB。
So I found in google the annotation @xmlSeeAlso
I used this, but I don´t know if it´s correct... 所以我在google中找到了注释@xmlSeeAlso
我也用过这个,但我不知道它是否正确......
Because the stackTrace prints this error 因为stackTrace会输出此错误
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 11 counts of IllegalAnnotationExceptions
Class has two properties of the same name "bairro"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getBairro()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.bairro
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "cep"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getCep()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.cep
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "codigoMunicipio"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getCodigoMunicipio()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.codigoMunicipio
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "codigoPais"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getCodigoPais()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.codigoPais
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "complemento"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getComplemento()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.complemento
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "logradouro"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getLogradouro()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.logradouro
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "municipio"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getMunicipio()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.municipio
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "numero"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getNumero()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.numero
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "pais"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getPais()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.pais
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "telefone"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getTelefone()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.telefone
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
Class has two properties of the same name "UF"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getUF()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.UF
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
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.<init>(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(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 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.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 nfeProc.Teste.main(Teste.java:15)
So what´s wrong? 那有什么不对吗?
UPDATE UPDATE
put my code in pastebin.. To explain for you, my classes emitente and destinatario have a field with a endereco (adress) from from enderecoEmitente and enderecoDestinario and they extends from Endereco http://pastebin.com/db0M3rG5 把我的代码放在pastebin中。为了向您解释,我的类emitente和destinatario有一个带有来自enderecoEmitente和enderecoDestinario的endereco(地址)的字段,它们来自Endereco http://pastebin.com/db0M3rG5
Using your code on paste bin all I had to do was add @XmlAccessorType(XmlAccessType.FIELD) to the
Endereco` to get everything to work correctly (since that class has annotated fields). 在粘贴bin上使用你的代码,我所要做的就是将@XmlAccessorType(XmlAccessType.FIELD) to the
添加@XmlAccessorType(XmlAccessType.FIELD) to the
Endereco`以使一切正常工作(因为该类具有注释字段)。
@XmlAccessorType(XmlAccessType.FIELD)
public class Endereco {
The problem you are encountering isn't related to inheritance but to the fact that you have annotated the field instead of the property. 您遇到的问题与继承无关,而是与您注释字段而不是属性有关。 By default a JAXB implementation will treat public properties and annotated fields as mapped (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html ), this is causing the conflict in your use case: 默认情况下,JAXB实现会将公共属性和带注释的字段视为已映射(请参阅: http : //blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html ),这会导致您使用冲突案件:
Class has two properties of the same name "pais"
this problem is related to the following location:
at public java.lang.String nfeProc.NFe.infNFe.Endereco.getPais()
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
this problem is related to the following location:
at private java.lang.String nfeProc.NFe.infNFe.Endereco.pais
at nfeProc.NFe.infNFe.Endereco
at private nfeProc.NFe.infNFe.Endereco nfeProc.NFe.infNFe.emit.Emitente.endereco
at nfeProc.NFe.infNFe.emit.Emitente
at private nfeProc.NFe.infNFe.emit.Emitente nfeProc.NFe.infNFe.InfNFe.emitente
at nfeProc.NFe.infNFe.InfNFe
at private nfeProc.NFe.infNFe.InfNFe nfeProc.NFe.NFe.infNFe
at nfeProc.NFe.NFe
at private nfeProc.NFe.NFe nfeProc.NFeProc.nfe
at nfeProc.NFeProc
The solution is to either annotate the get
method (or set
method), or annotate the field and specify @XmlAccessorType(XmlAccessType.FIELD)
at the class or package level. 解决方案是注释get
方法(或set
方法),或者注释该字段并在类或包级别指定@XmlAccessorType(XmlAccessType.FIELD)
。
I have written a lot about JAXB and inheritance, you may find the following helpful: 我写了很多关于JAXB和继承的文章,你可能会发现以下内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.