杰克逊的序列化问题

问题描述

我在下面的类的序列化/反序列化时遇到麻烦。

我的Data类包含其他类的列表。

当我在Data类中调用序列化/反序列化方法时,出现以下错误

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 @R_973_4045@ion?)

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

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

即使我设置了objectMapper.registerModule(new Jdk8Module());字段,也无法正确地对Optional字段进行序列化


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

任何指针将不胜感激!


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;
}