Deserialize a map to POJO object containing a List of Immutable Type

275 Views Asked by At

While building an API, I was trying to deserialize a map object into an immutableInterface KycMetadata(which contains a list of another immutable Interface InternalComponent) Details are as follows, kindly help

KycMetadata.interface

@Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(as = ImmutableKycMetadata.class)
@JsonDeserialize(as = ImmutableKycMetadata.class)
public interface KycMetadata {

    @JsonProperty("documentNameStringId")
    String documentNameStringId();

    @JsonProperty("documentType")
    String documentType();

    @JsonProperty("documentTypeStringId")
    String documentTypeStringId();

    @JsonProperty("components")
    List < InternalComponent > components();
}

InternalComponent.interface

@Modifiable
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @Type(value = ImmutableInternalTextComponent.class, name = "TEXT"),
    @Type(value = ImmutableInternalDateComponent.class, name = "DATE"),
    @Type(value = ImmutableInternalBooleanComponent.class, name = "BOOLEAN"),
    @Type(value = ImmutableInternalSelectComponent.class, name = "SELECT"),
    @Type(value = ImmutableInternalDataFilesComponent.class, name = "DATAFILES"),
    @Type(value = ImmutableInternalBlurbComponent.class, name = "BLURB")

})

@SuppressWarnings("checkstyle:indentation")
@Value.Immutable
@JsonDeserialize(as = ImmutableInternalComponent.class)
public interface InternalComponent {

    @JsonProperty("name")
    String name();
}

I am trying to deserialize in code like this:

final ObjectMapper mapper = new ObjectMapper(); // jackson's objectmapper
final KycMetadata pojo = mapper.convertValue(map, KycMetadata.class);
return pojo;

map has this content:

{documentNameStringId=kyc_xborder_sdc_documentName_passport, components=[{labelStringId=kyc_xborder_sdc_selection_issuingCountry, name=COUNTRY_OF_ISSUE, valuesType=COUNTRIES, type=SELECT, prompt=kyc_xborder_sdc_selection_country}, {labelStringId=kyc_xborder_sdc_text_passportNumber, name=ID_NUMBER, shadowTextStringId=kyc_xborder_sdc_text_idNumber_shadowText, type=TEXT, validationRegex=^[A-Za-z0-9]{5,20}$}, {labelStringId=kyc_xborder_sdc_date_expirationDate, dateType=MONTH_YEAR, name=EXPIRATION_DATE, validRange=FUTURE, type=DATE}], documentType=ID number, documentTypeStringId=kyc_xborder_sdc_documentType_idNumber}

This is the error I am getting:


java.lang.IllegalArgumentException: Cannot construct instance of KycMetadata (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]
    at com.amazon.coral.bobcat.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4314) ~[Bobcat-3.0.jar:?]

Please help as to how to resolve this.

0

There are 0 best solutions below