Jackson: "Unexpected token (VALUE_STRING), expected FIELD_NAME:" when deserializing an empty string instead of the expected object

I use Jackson to deserialize json responses from a server that I don't have. I use JsonTypeInfo annotations to handle polymorphic data types. Here I have the configuration on the base type ( Thingin this case):

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Thing.class, name = "Thing"),
        @JsonSubTypes.Type(value = FancyThing.class, name = "FancyThing")
})

Everything works fine until the server returns an empty string, where I expect one of these types, and then I get one of the following:

org.codehaus.jackson.map.JsonMappingException: Unexpected token (VALUE_STRING), expected FIELD_NAME: missing property 'type' that is to contain type id (for class com.stackoverflow.Thing)

Is there a recommended way to handle such cases? As I said, I do not control the server, so I have to deal with this client side. I would be better off doing this by tuning ObjectMapper, but ObjectMapper#configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)not working as I expected. Any ideas?

+4
1

, , , , - . , , javadocs, @JsonTypeInfo#defaultImpl, .

- :

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type"
        defaultImpl = EmptyThing.class)
@JsonSubTypes({
        @JsonSubTypes.Type(value = Thing.class, name = "Thing"),
        @JsonSubTypes.Type(value = FancyThing.class, name = "FancyThing")
})
public class Thing {

...

}

public class EmptyThing extends Thing {

    public EmptyThing(String s) {
        // Jackson wants a single-string constructor for the default impl
    }
}
+7

All Articles