宁静的jboss反序列化

时间:2015-03-20 14:03:18

标签: java json web-services rest jboss

我正在JBoss AS 7.1.1 Final中构建一个安静的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 {
}

我让这个课使用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"
    }
}

所以,我想创建一个基于atributte 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-structure.xml,并将pom.xml更改为提供;
  • 更改intire resteasy模块;
  • 更改为新的jackson 2.x;

认为jboss简单地忽略了那些注释。

TKS

1 个答案:

答案 0 :(得分:1)

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

我将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>

一切正常。有人可以解释为什么这有效吗?