gpt4 book ai didi

java - 如何在jaxb中使用继承?

转载 作者:数据小太阳 更新时间:2023-10-29 02:35:49 26 4
gpt4 key购买 nike

我有一个抽象类地址,以及两个继承自类地址的类..

A 类和 B 类。

所以我在 google 中找到了注释 @xmlSeeAlso 我使用了这个,但我不知道它是否正确...

因为 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)

怎么了?

最佳答案

更新

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

在粘贴箱上使用您的代码,我所要做的就是将 @XmlAccessorType(XmlAccessType.FIELD) 添加到 Endereco` 以使一切正常工作(因为该类具有带注释的字段)。

@XmlAccessorType(XmlAccessType.FIELD)
public class Endereco {

问题

您遇到的问题与继承无关,而是与您注释了字段而不是属性这一事实有关。默认情况下,JAXB 实现会将公共(public)属性和带注释的字段视为已映射(请参阅: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

解决方案

解决方法要么注解get方法(或set方法),要么注解字段并指定@XmlAccessorType(XmlAccessType.FIELD) 在类或包级别。

JAXB 和继承

我已经写了很多关于 JAXB 和继承的文章,您可能会发现以下内容对您有帮助:

关于java - 如何在jaxb中使用继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18615726/

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