We are using Jersey/Jackson to unmarshall JSON data to java DTOs. One of my DTO is an abstract class, and i would like to unmarshall the JSON data to one of his extended DTO. For example, assuming i have these DTOs :
public abstract class AnimalDTO{}
public class DogDTO extends AnimalDTO{}
public class CatDTO extends AnimalDTO{}
I would like to unmarshall this JSON data:
{Zoo: {Animals:[{"type"="DogDTO", "code"="001", "name"="chihuahua"}, {"type"="CatDTO", "code"="002", "name"="felix"}]}}
As "type" would give the type of DTO i would like to unmarshall to. But it seems that this property isn't considered. Is there something I missed, or mistook in the JSON syntax?
Thanks.
In your case you should use @JsonTypeInfo annotation.
For more information, please see below links:
Using above links I have created a simple example which serialize POJO objects with class names:
import java.io.StringWriter;
import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonProgram {
    public static void main(String[] args) throws Exception {
        DogDTO dog = new DogDTO();
        dog.setCode("001");
        dog.setName("chihuahua");
        CatDTO cat = new CatDTO();
        cat.setCode("002");
        cat.setName("felix");
        Zoo zoo = new Zoo();
        zoo.setAnimals(new AnimalDTO[] { dog, cat });
        Data data = new Data();
        data.setZoo(zoo);
        ObjectMapper objectMapper = new ObjectMapper();
        StringWriter writer = new StringWriter();
        objectMapper.writeValue(writer, data);
        System.out.println(writer);
    }
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
abstract class AnimalDTO {
    private String code;
    private String name;
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "AnimalDTO [code=" + code + ", name=" + name + "]";
    }
}
class DogDTO extends AnimalDTO {
}
class CatDTO extends AnimalDTO {
}
class Zoo {
    @JsonProperty(value = "Animals")
    private AnimalDTO[] animals;
    public AnimalDTO[] getAnimals() {
        return animals;
    }
    public void setAnimals(AnimalDTO[] animals) {
        this.animals = animals;
    }
    @Override
    public String toString() {
        return "Zoo [animals=" + Arrays.toString(animals) + "]";
    }
}
class Data {
    @JsonProperty(value = "Zoo")
    private Zoo zoo;
    public Zoo getZoo() {
        return zoo;
    }
    public void setZoo(Zoo zoo) {
        this.zoo = zoo;
    }
    @Override
    public String toString() {
        return "Data [zoo=" + zoo + "]";
    }
}
This program prints:
{"Zoo":{"Animals":[{"type":"DogDTO","code":"001","name":"chihuahua"},{"type":"CatDTO","code":"002","name":"felix"}]}}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With