java – Generic Abstract类的Custom Jackson反序列化

我在尝试反序列化以下类时遇到问题:

public class MetricValuesDto {

    private Map<MetricType, MetricValueDto<?>> metricValues;

    public MetricValuesDto() {
    }

    public MetricValuesDto(Map<MetricType, MetricValueDto<?>> metricValues) {
        this.metricValues = metricValues;
    }

    public Map<MetricType, MetricValueDto<?>> getMetricValues() {
        return metricValues;
    }

    public void setMetricValues(Map<MetricType, MetricValueDto<?>> metricValues) {
        this.metricValues = metricValues;
    }
}

我的通用抽象类:

public abstract class MetricValueDto<T> {

    private T value;
    private MetricTrend trend;

    public MetricValueDto(T value, MetricTrend trend) {
        this.value = value;
        this.trend = trend;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public MetricTrend getTrend() {
        return trend;
    }

    public void setTrend(MetricTrend trend) {
        this.trend = trend;
    }
}

我有两个实现MetricValueDto的具体类:

IntMetricValueDto:

public class IntMetricValueDto extends MetricValueDto<Integer> {

    public IntMetricValueDto(Integer value, MetricTrend trend) {
        super(value, trend);
    }
}

FloatMetricValueDto:

public class FloatMetricValueDto extends MetricValueDto<Float> {

    public FloatMetricValueDto(Float value, MetricTrend trend) {
        super(value, trend);
    }
}

知道将MetricValueD反序列化的正确策略是什么,所以我可以通过ObjectMapperRestTemplate解析它?每当我跑:

restTemplate.exchange("myEndpoint", HttpMethod.GET, entity, DataCollectionEventDto.class);

我明白了

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.resson.dto.MetricValueDto: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

DataCollectionEventDto:

public class DataCollectionEventDto {

    private List<MapLayerDto> mapLayers;

    @JsonUnwrapped
    private MetricValuesDto metricValues;

    public List<MapLayerDto> getMapLayers() {
        return mapLayers;
    }

    public void setMapLayers(List<MapLayerDto> mapLayers) {
        this.mapLayers = mapLayers;
    }

    public MetricValuesDto getMetricValues() {
        return metricValues;
    }

    public void setMetricValues(MetricValuesDto metricValues) {
        this.metricValues = metricValues;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

我基本上已经在网上尝试了一切,但我无法使其发挥作用;任何建议都会有所帮助.

最佳答案
使用JsonSubTypes注释和JsonTypeInfo来指示子类型.属性属性JsonTypeInfo用于区分不同的子类.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "typ")
@JsonSubTypes({
        @JsonSubTypes.Type(value = IntMetricValueDto.class, name = "INT"),
        @JsonSubTypes.Type(value = FloatMetricValueDto.class, name = "FLT")}
public abstract class MetricValueDto<T> {

    private T value;
    private MetricTrend trend;
    ...
}

转载注明原文:java – Generic Abstract类的Custom Jackson反序列化 - 代码日志