In given class Base
which is extended by Ext
class. Serialization works perfect but issue is when trying to deserialize the serialized string back to Ext
class. I want to deserialize back to Ext
class including the all the Base class fields.
@Data, @NonFinal, @Value are all lombok annotations.
@Data
public class Base {
private String foo;
public Base( String foo) {
this.foo = foo;
}
}
@Value
@NonFinal
public class Ext extends Base {
private String bar;
public Ext(String foo, String bar) {
super(foo);
this.bar = bar;
}
}
Method to Deserialize
@Test
void shouldDeserialize() throws IOException {
ObjectMapper mapper = new ObjectMapper();
Ext ext = new Ext("foo", "bar");
String serializedExt = mapper.writeValueAsString(ext);
System.out.println(serializedExt); // {"foo":"foo","bar":"bar"}
// Throws err
base = mapper.readValue(serializedExt, Ext.class);
}
Error: com.fasterxml.jackson.databind.exc.InvalidDefinitionException:Cannot construct instance of ..Inhertence.Ext(no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"foo":"foo","bar":"bar"}"; line: 1, column: 2]