Search code examples
javajacksonjackson-databind

Deserializing abstract generic class in Java with Jackson


I am sending a POST request with the Sample JSON request body:

"name" : "jeff",
"country" : "US",
"content" : {
    "subject" : "Test-Subject",
    "body" : "Test-body"
} 

The class that is this JSON is deserialized into:

@Introspected
@Builder
@Data
public class Template<T extends Content> {
    String name;
    String country;
    T content;
}

Content looks like this:

@Introspected
@Superbuilder
@Getter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor(onConstructor_ = @JsonCreator)
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, property="content")
@JsonSubTypes(@JsonSubTypes.Type(value = EmailContent.class, name="EmailContent"))
public abstract class Content {
    private String body;
}

This is what I want T content to deserialize into:

@Introspected
@Superbuilder
@Getter
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor(onConstructor_ = @JsonCreator)
public class EmailContent extends Content {
    private String subject;
}

I have everything working in Template EXCEPT the generic content type which is giving me trouble no matter what JsonTypeInfo I use. I am trying to deserialize it into an EmailTemplate class. I have other classes extending from content so I am not looking to use @JsonDeserialize.


Solution

  • Solved...it was due to using graalVM which supports partial reflection. I fixed this by adding the @ReflectiveAccess annotation alongside lombok's @Jacksonized annotation for deserializing builder types.