简体   繁体   中英

com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id '[' as a subtype

I have an abstract class called Fruit and I put the @JsonTypeInfo and @JsonSubTypes on it as follow:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "fruits")
@JsonSubTypes({
    @Type(value = Apple.class, name = "sandbox.Apple"),
    @Type(value = FruitGroup.class, name = "sandbox.FruitGroup")
})
public abstract class Fruit {

    public abstract String getName();

    @Override
    public String toString() {
        return "Fruit [getName()=" + getName() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
    }
}

And my derived class looks like this

@JsonTypeName("sandbox.Apple")
public class Apple extends Fruit {

    private String _name;

    public void setName(String name) {
        _name = name;
    }

    @Override
    public String getName() {
        return _name;
    }

======[UPDATED]====== I also have class FruitGroup that extends Fruit and contains Array of Fruit.

@JsonTypeName("sandbox.FruitGroup")
public class FruitGroup extends Fruit {
private Fruit[] _Fruit;
private String _name;
private String _category;

public Fruit[] getFruit() {
    return _Fruit;
}

public void setFruits(Fruit[] fruits) {
    _Fruit = fruits;
}

public void setName(String name) {
    _name = name;
}

@Override
public String getName() {
    return _name;
}

public void setCategory(String category) {
    _category = category;
}

public String getCategory() {
    return _category;
}

}

When I tried to deserialize the jsontext into Fruit object, I found the following exception:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id '[' as a subtype of `sandbox.FruitGroup`: known type ids = [FruitGroup, sandbox.Apple] at [Source: (String)"{"fruits":[["sandbox.Apple",{"name": "Apple"}]],"name": "Fruit Group"}"; line: 1, column: 11]
at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)

The jsontext [UPDATED] actually was generated by jackson version 2.10.2 and I didn't put any JSON annotations on my classes formerly. After I upgrade the jackson version to 2.11.0, I also update my abstract class to put the JSON annotations in it. Then I tried to deserialize it by using jackson version 2.11.0, but I got an error instead. Could you guys help me to solve this issue? Here's my jsontext

{
    "fruit": [
        [
            "sandbox.Apple",
            {
                "name": "Apple1"
            }
        ]
    ],
    "name": "Group of apples",
    "category": "Sweet fruit"
}

Your code fails because in your json file you have "fruits": [] , so you are associating fruits with an json array while in your abstract Fruit class you expect the "fruits" property referring to the name of the subclass you want to deserialize your json. So if you modify your json like below :

{
    "fruits": "sandbox.Apple",
    "name": "MyApple"
}

You can then deserialize it to the Apple subclass of the Fruit class:

Fruit fruit = mapper.readValue(src, Fruit.class);
System.out.println(fruit); //<-- Apple(_name=MyApple)

So you can either decide to change your json format to match your java classes or decide to change your java classes to match your json file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM