Using Jackson ObjectMapper to serialize the subclass name into JSON, not the superclass

前端 未结 3 2101
执笔经年
执笔经年 2021-02-14 09:58

In the following Jackson/Java code that serializes objects into JSON, I am getting this:

{\"animal\":{\"x\":\"x\"}}

However, what I actually wa

3条回答
  •  长发绾君心
    2021-02-14 10:39

    As per this announement, Jackson 1.5 implements full polymorphic type handling, and trunk now has that code integrated.

    There are two simple ways to make this work:

    • Add @JsonTypeInfo annotation in supertype (Animal here), OR
    • Configure object mapper by calling ObjectMapper.enableDefaultTyping() (but if so, Animal needs to be abstract type)

提交回复
热议问题