1

我有一个基类如下:

public class Criteria {
    private CriteriaType type;
        //getters & setters
        public enum CriteriaType {
        Condition, Medication
    }
}

和派生类

public class ConditionCriteria extends Criteria {
    private String a;
        //getters & setters
}

public class MedicationCriteria extends Criteria {
    private String b;
        //getters & setters
}

和另一个班级

public class CriteriaGroup {
    Criteria criteria;
        //getters & setters
}

CriteriaGroup我使用 Jackson将一个序列化的 JSON 类字符串发送到服务器以进行(反)序列化。服务器上的服务如下所示:

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createCriteriaGroup(CriteriaGroup criteriaGroup) {...

但是,Jackson 不会将反序列化的 JSON 转换为派生类之一。如何根据类的type字段实现这种映射Criteria

谢谢..

在反序列化 ConditionCriteria 的 JSON 时出现这样的异常:

SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "a" (Class Criteria), not marked as ignorable
 at [Source: org.eclipse.jetty.server.HttpInput@3c92218c; line: 1, column: 92] (through reference chain: CriteriaGroup["criteria"]->Criteria["a"])
        at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
        at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)

……

4

1 回答 1

0

您将需要添加@JsonTypeInfoCriteria启用其他类型信息,以支持多态类型。你应该能够找到这样的例子(只是谷歌“jackson JsonTypeInfo”)。

于 2013-01-26T00:27:58.727 回答