简体   繁体   中英

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException - Jackson Library

I get the below error when I try to deserialize Automobile class. Jackson is trying to search for the field in child element in parent class. How can I make sure jackson uses the appropriate child type for deserialization? I believe I need to use miixins / customer converters. But i'm not sure how to use them in this specific scenario.

Note: In my case all classes except TestMain are in a jar file and I cannot modify the source files.

Error

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "color" (class com.salesportal.proxy.Car), not marked as ignorable (one known property: "name"])

Automobile.java

public class Automobile {

    private Car[] cars;

    public Car[] getCars() {
        return cars;
    }

    public void setCars(Car[] cars) {
        this.cars = cars;
    }



}

Car.java

public class Car {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

Corolla.java

public class Corolla extends Car {

    private String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }



}

TestMain.java

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJSON {

    public static void main(String[] args) throws IOException{

        ObjectMapper mapper = new ObjectMapper();

        Automobile automobile = new Automobile();
        Corolla corolla = new Corolla();
        corolla.setName("Toyota");
        corolla.setColor("Silver Grey");
        automobile.setCars(new Corolla[]{corolla});

        System.out.println(mapper.writeValueAsString(automobile));

        Automobile auto = mapper.readValue(mapper.writeValueAsString(automobile), Automobile.class);
    }

}

JSON String

{"cars":[{"name":"Toyota","color":"Silver Grey"}]}

Vicky , you can use sub type annotations in JACKSON. The below works for me , with just this change

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = Corolla.class, name = "color") })
public class Car {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Automobile class doesn't have color property.

Change this :

Automobile auto = mapper.readValue(mapper.writeValueAsString(automobile), Automobile.class);

to this :

Corolla auto = mapper.readValue(mapper.writeValueAsString(automobile.getCars()), Corolla .class);

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