0

I have a class where I've applied @JsonTypeInfo and JsonSubTypes as follows,

public class RequestDto {
    private List<IFoo> pageConfig = new ArrayList<>();
    ...
}
@JsonTypeInfo(
    use = Id.NAME,
    include = As.PROPERTY,
    property = "iSlotType",
    visible = true
)
@JsonSubTypes({
    @Type(value = LSlot.class),
    @Type(value = WSlot.class)
})
public interface IFoo {
   ...
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonTypeName("lSlot")
public class LSlot implements IFoo{
    private String id;
}

Interesting thing is that when I serialise an object of RequestDto, serialization is as expected,

{
    "pageConfig": [
        {
            "iSlotType": "lSlot",
            "id": "1001"
        },
        {
            "iSlotType": "wSlot",
            "id": "23",
            "parentId": "34"
        }
    ]
}

but when I serialize only the list as,

objectMapper.writeValueAsString(requestDto.getPageConfig);

I get the string without typeInfo IFoo types,

[
    {
        "id": "1001"
    },
    {
        "id": "23",
        "parentId": "34"
    }
]

How do I get the typeInfo here too ?

Utsav10
  • 400
  • 2
  • 11

0 Answers0