6

I have the following object structure:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type")
@JsonSubTypes({ @JsonSubTypes.Type(value = Dog.class, name = "dog") })
interface Animal {
    String name = null;
}    

@JsonTypeName("dog")
class Dog implements Animal {
    public double barkVolume;
    public String name;
}

class Zoo {
    public Animal animal;
    public Dog thisIsDog;
}

and I want to deserialize the following JSON file into Zoo class:

{
    "animal": {
        "@type": "dog",
        "barkVolume": 12.0,
        "name": "my dog;"
    },
    "thisIsDog": {
        "barkVolume": 12.0,
        "name": "my dog;"
    }
}

as follows:

Zoo newObject = new ObjectMapper().readValue(jsonString, Zoo.class);

but I get this error:

Missing type id when trying to resolve subtype of [simple type, class com.test.Dog]: 
missing type id property '@type' (for POJO property 'thisIsDog')

and if I add "@type": "dog", to thisIsDog node of course everything work... but I don't want to. I believe Jackson should have the info about the type because it's explicitly defined in Zoo class, no?

I feel I'm missing an attribute of some sort.. but can't figure out which one. Any help would be appreciated.

avs099
  • 10,937
  • 6
  • 60
  • 110

0 Answers0