7

I am using Jackson to convert a big Json string into various classes and subclasses.

I have a list of objects, each containing a node object, a last result object and a children array. The children array contains a list of objects with exactly the same setup. This goes on for 3 or 4 layers.

Each layers' node is of a different subclass, which all extend from a node superclass. I have annotated the superclass node with following annotations:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Type1ResponseDto.class, name = "Type1"),
    @JsonSubTypes.Type(value = Type2ResponseDto.class, name = "Type2"),
    @JsonSubTypes.Type(value = Type3ResponseDto.class, name = "Type3"),
    @JsonSubTypes.Type(value = Type4ResponseDto.class, name = "Type4"),
    @JsonSubTypes.Type(value = Type5ResponseDto.class, name = "Type5")
})

This seems to work, since all subclasses get mapped.

However, this somehow results in the "type" property being set to null.

Any ideas as to why this happens?

Wim Berchmans
  • 271
  • 2
  • 12
  • Does this answer your question? [Jackson - @JsonTypeInfo property is being mapped as null?](https://stackoverflow.com/questions/33611199/jackson-jsontypeinfo-property-is-being-mapped-as-null) – Kampaii May 26 '22 at 10:07

1 Answers1

10

I needed to add visible=true for the type property to show up:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type",visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = Type1ResponseDto.class,name =  "Type1"),
    @JsonSubTypes.Type(value = Type2ResponseDto.class, name = "Type2"),
    @JsonSubTypes.Type(value = Type3ResponseDto.class, name = "Type3"),
    @JsonSubTypes.Type(value = Type4ResponseDto.class, name = "Type4")
})
Jasper
  • 11,590
  • 6
  • 38
  • 55
Wim Berchmans
  • 271
  • 2
  • 12
  • Which version of Jackson does this answer apply to? I'm using 1.9.9 and I don't have those options, and I'm having this exact issue. – TheIcemanCometh May 07 '15 at 11:17