序列化包含接口的对象

时间:2017-06-02 05:05:41

标签: java json rest serialization gson

我有一个类,其对象是我尝试序列化/反序列化:

public class PlanDetails implements Serializable {

private static final long serialVersionUID = -5344035164109142313L;

    @JsonProperty("reserved_volumes")
    private List<ReservedVolume> reservedVolumes;

    private String description; 

    //getter, setter, toString()

ReservedVolume定义为:

public class ReservedVolume implements Serializable {

private static final long serialVersionUID = 4157996827172339903L;

@JsonProperty("reserved_volume_projection")
private GeoShape reservedVolumeProjection;

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
@JsonProperty("effective_time_begin")
private Date effectiveTimeBegin;

private String notes;

private Double altitude;

// getter, setter, toString()

现在这个GeoShape是一个具有2个(将来可能更多)实现的接口。 我怎样才能反序化呢?我期待这个对象作为REST端点的请求体。作为List<ReservedVolume>,用户应该能够发送GeoShape的任何实现组合。需要将其反序列化以进行解析。

写了一个方法:

public static PlanDetails fromJson(String jsonString) {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    return gson.fromJson(jsonString, new TypeToken<PlanDetails>() {
    }.getType());
}

但反序列化时出现Interface can't be instantiated!错误。有什么帮助吗?

0 个答案:

没有答案