gpt4 book ai didi

java - Restful jboss 反序列化

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:07 26 4
gpt4 key购买 nike

我正在 JBoss AS 7.1.1 Final 中构建一个 Restful Web 服务。它工作得很好,直到我需要一个自定义解串器。代码:

@Entity
@DiscriminatorColumn(name = "tipo")
@Table(name = "debitavel", schema = "despesas_db")
@Inheritance(strategy = InheritanceType.JOINED)
@JsonDeserialize(using = DebitavelDeserializer.class)
@JsonSerialize(using = DebitavelSerializer.class)
public abstract class Debitavel {

}

该类有 2 个子类:

@Entity
@Table(name = "conta", schema = "despesas_db")
@DiscriminatorValue(value = "CONTA")
public class Conta extends Debitavel {
}

@Entity
@DiscriminatorValue(value = "CONTA")
@Table(name = "cartao", schema = "despesas_db")
public class CartaoCredito extends Debitavel {
}

我这个类(class)使用了 Debitavel:

@Entity
@Table(name = "despesa", schema = "despesas_db")
@DiscriminatorValue(value = "D")
public class Despesa {

@Column
private String descricao;

@ManyToOne
@JoinColumn(name = "debitavel_id")
protected Debitavel debitavel;

}

所以在页面中我发送了一个像这样的json

{
"descricao": "TESTE",
"debitavel": {
"id": 101,
"descricao": "Conta1",
"cor": "#ff0000",
"tipo": "CONTA"
}
}

所以,我想要一个基于属性 tipo 创建 CartaoCredito 或 Conta 实例的类,所以我构建了一个反序列化器:

public class DebitavelDeserializer extends JsonDeserializer<Debitavel> {

@Override
public Debitavel deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
//staff to create Conta or CartaoCredito
}

}

还有我的 pom.xml:

         <dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>

但是,当我尝试部署应用程序时,jboss 说 Debitavel 是一个抽象类,它无法构建实例。我寻找解决方案并尝试了几种解决方案:

  • 使用 jboss 中的模块创建 jboss-deployment-struct.xml 并将 pom.xml 更改为提供的;
  • 更改整个resteasy模块;
  • 更改为新的 jackson 2.x;

可以看出,jboss 简单地忽略了这些注释。

谢谢

最佳答案

我解决了......但说实话我不明白为什么,所以让我们看看:

我将 pom.xml 更改为

        <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.2.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>2.3.2.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>2.3.2.Final</version>
</dependency>

一切都很好。有人可以解释为什么这有效吗?

关于java - Restful jboss 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29168372/

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