Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

jersey - Java unmarshilling JSON data containg abstract type

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"}]}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...