Skip to content
Advertisement

Custom Jackson Deserialization of a Generic Abstract class

I am having issues when trying to deserializing the following class:

JavaScript

My generic abstract class:

JavaScript

I have two concrete classes which implement MetricValueDto:

IntMetricValueDto:

JavaScript

FloatMetricValueDto:

JavaScript

Any idea of what’s the correct strategy to deserialize MetricValueDto so I can parse it through ObjectMapper or an RestTemplate? Whenever I run:

JavaScript

I get

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:

JavaScript

I have basically tried everything on web and I could not make it work; any suggestion would be helpful.

Advertisement

Answer

While JsonTypeInfo works, and adds implementation-specific detail to the response, which later might add confusion to the API client.

I ended up implementing a custom StdDeserializer:

JavaScript

The code ended up to being more complex than JsonTypeInfo, but the API client is unaware of implementation-specific details.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement