gpt4 book ai didi

java - JaxB 无法转换为 java.lang.Integer

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:24 27 4
gpt4 key购买 nike

我有一个抽象类:



<pre><code>@MappedSuperclass
public abstract class BaseEntity<K>
{

@Temporal(value = TemporalType.TIMESTAMP)
private Date cadastrado;
@Temporal(value = TemporalType.TIMESTAMP)
private Date modificado;
@Column(length = 30)
private String ip;
private String autorModificacao;

public abstract K getId();

public abstract void setId(K id);
</code></pre>

<p>...</p>

<p>and a derived class:</p>

<pre><code>@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Pessoa extends BaseEntity<Integer> implements Serializable {

@Id
@ColumnGridPF
@GeneratedValue(strategy = GenerationType.AUTO, generator = "pessoa")
private Integer id;

....

@Override
Integer getId() {
return id;
}

@Override
public void setId(Integer id) {
this.id = id;
}
</code></pre>

<p>....</p>

<p>when my application try to unmarshall the object, I get an error</p>

<p>**</p>

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to java.lang.Integer
at br.com.sigmaonline.entity.cadastro.pessoa.Pessoa.setId(Pessoa.java:46)
at br.com.sigmaonline.entity.common.generic.BaseEntity$JaxbAccessorM_getId_setId_java_lang_Object.set(MethodAccessor_Ref.java:60)

**

谁能帮帮我?

最佳答案

默认情况下,当您的 JAXB (JSR-222)实现正在为 Pessoa 创建元数据,它还将为父类(super class) BaseEntity 创建元数据。由于默认情况下 JAXB 将属性视为已映射,因此它会认为它具有一个名为 id 且类型为 Object 的属性。当 JAXB 不知道属性的类型时,它会将其转换为 DOM Element。这会导致 ClassCastException

解决方案

解决方案实际上取决于您是否希望将 BaseEntity 视为继承层次结构的一部分(参见:http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html)。但我建议的是在 BaseType 上利用 @XmlTransient@XmlAccessorType(XmlAccessType.NONE) 来删除有问题的属性:

关于java - JaxB 无法转换为 java.lang.Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16565166/

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