I have Json Book requests defined as
{
"book" : {
"type" : "PRINT",
"value" : {
"weight" : "1lb"
}
}
}
or
{
"book" : {
"type" : "EBOOK",
"value" : {
"size" : "1MB"
}
}
}
Value is a polymorphic object.
I defined my Java POJOs as below. I am defining value
as polymorphic object.
@Getter
@Builder
@JsonDeserialize(builder = BookRequest.BookRequestBuilder.class)
public class BookRequest
{
@NonNull
private Book book;
}
Book is defined as
@Builder
@Getter
@JsonDeserialize(builder = Book.BookBuilder.class)
public class Book
{
@NonNull
private BookType type;
@NonNull
private BookValue value;
}
BookValue is defined as a polymorphic object.
public interface BookValue
{
}
For which PrintBook is a type
@Getter
@Builder
@JsonDeserialize(builder = PrintBook.PrintBookBuilder.class)
public class PrintBook implements BookValue
{
private String weight;
}
Book type is defines as an enum
@Getter
public enum BookType
{
EBOOK,
PRINT
}
When I am trying to deserialize a PRINT book json with the below code
public deserializePrintBook{
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
try {
JsonNode node = createJsonNodeFromFile("src/jacksonannotations/resources/sampleBook.json");
BookRequest br = mapper.treeToValue(node, BookRequest.class);
System.out.println(br);
}
catch (Exception e ) {
e.printStackTrace();
}
}
public static JsonNode createJsonNodeFromFile(final String filePath) throws Exception
{
ObjectMapper objectMapper = new ObjectMapper();
File file = new File(filePath);
JsonNode testMessageEnvelope = objectMapper.readValue(
file,
JsonNode.class);
return testMessageEnvelope;
}
But, I am getting this error
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
jacksonannotations.books.BookValue
(no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: jacksonannotations.books.BookRequest$BookRequestBuilder["book"]->jacksonannotations.books.Book$BookBuilder["value"])
I already looked into Cannot construct instance of - Jackson, but that didn't help.
Can someone help me understand if I am modeling the Java pojos in the above example correctly?
Thanks, Pavan