杰克逊序列化问题

Jackson Serialization Problems

我在下面 serializing/deserializing 我的 class 遇到了一些麻烦。

我的 Data class 拥有其他 class 的列表。

当我调用 Data class 中的 serialize/deserialize 方法时,出现以下错误:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.amazon.rancor.storage.types.ChildData: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

错误来自反序列化方法。但我也相信序列化工作不正常。这是序列化的 Data 对象的样子:

{childData:[{zipCode:{present:true},countryCode:"US"}]

即使我设置了 objectMapper.registerModule(new Jdk8Module()); 字段,但可选字段没有被正确序列化


我似乎无法弄清楚我做错了什么。也许我需要在 ChildDataChildDataV2 class 中更改一些内容。但我不确定是什么。

如有指点,将不胜感激!


public class Data {
    private List<ChildData> childData;
    private List<ChildDataV2> childDataV2;

    private static ObjectMapper objectMapper;
    static {
        objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.registerModule(new Jdk8Module());
    }

    public Data() { }

    @JsonCreator
    public Data(@JsonProperty("childData") final List<ChildData> childData,
                          @JsonProperty("childDataV2") final List<ChildDataV2> childDataV2) {
        this.childData = childData;
        this.childDataV2 = childDataV2;
    }

    public List<ChildData> getChildData() {
        return childData;
    }
    
    public void setChildData(final List<ChildData> childData) {
        this.childData = childData;
    }

    public List<ChildDataV2> getChildDataV2() {
        return childDataV2;
    }

    public void setChildDataV2(final List<ChildDataV2> childDataV2) {
        this.childDataV2 = childDataV2;
    }

    public String serialize() {
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Failed to serialize. Data: " + this, e);
        }
    }

    public Data deSerialize(final String data) {
        try {
            return objectMapper.readValue(data, Data.class);
        } catch (IOException e) {
            throw new RuntimeException("Failed to deserialize. Data" + data, e);
        }
    }
}
public class ChildData {
    private final String countryCode;
    private final Optional<String> zipCode;

    public ChildData(final String countryCode, final Optional<String> zipCode) {
        this.countryCode = countryCode;
        this.zipCode = zipCode;
    }

    public Optional<String> getZipCode() {
        return zipCode;
    }

    public String getCountryCode() {
        return countryCode;
    }
}
public class ChildDataV2 extends ChildData {
    private final Object struct;

    public ChildDataV2(final String cc, final Optional<String> postalCode,
        final Object struct) {
        super(cc, postalcode);
        this.struct = struct;
    }
}

异常很明显吧?您需要为 ChildData 添加默认构造函数或像这样注释现有构造函数:

@JsonCreator
public ChildData(@JsonProperty("countryCode") String countryCode, @JsonProperty("zipCode") Optional<String> zipCode) {
    this.countryCode = countryCode;
    this.zipCode = zipCode;
}