2

我有一个关于 Jackson 的 Json 反序列化问题(编辑:2.0.4 版本)。我想将包含其他 bean 列表的 Bean 序列化为 string ,保存此字符串,然后稍后反序列化此字符串。我使用一些基类及其子类型。基础类Parent是一个抽象类,有getter和setter两个属性,这个类还有一个抽象方法getType()。其他抽象类 AbstractChild 继承自类 Parent 。这个类也有属性和 isExportEnabled() 抽象方法。我没有问题,如果这个 Bean 将被序列化。我在Parent上使用以下注释class *@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@cls")* 将生成字符串。但是反序列化失败:会抛出异常“Unrecognized field "type"”。但我需要这个属性!我试图在抽象方法上设置 @JsonProperty("type") ,这没有效果。请帮我。

编辑:如果我引入私有字段“type”(父)和“exportEnabled”(AbstractChild),那么它可以正常运行。 PS 例外

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“类型”(类 tst.SimpleTestMain$FirstChild),未标记为可忽略(4 个已知属性:、“id”、“maxCount”、“code”、“minCount” "]) 在 [来源:java.io.StringReader@1ad9fa; 行:1,列:125](通过引用链:tst.Fam["members"]->tst.FirstChild["type"])在 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java :79) 在 com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:568)

…和示例类

package tst;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SimpleTestMain {

    enum Type {
        TYPE_A, TYPE_B
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@cls")
    public static abstract class Parent {

        private int id;

        private String code;

        public Parent() {

        }
        @JsonProperty("type")
        // First abstract getter
        public abstract Type getType();

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

    }

    public static abstract class AbstractChild extends Parent {

        private int minCount;

        private int maxCount;

        public AbstractChild() {

        }

        // Second abstract method: boolean used
        public abstract boolean isExportEnabled();

        public int getMinCount() {
            return minCount;
        }

        public void setMinCount(int minCount) {
            this.minCount = minCount;
        }

        public int getMaxCount() {
            return maxCount;
        }

        public void setMaxCount(int maxCount) {
            this.maxCount = maxCount;
        }

    }

    public static class FirstChild extends AbstractChild {

        @Override
        public boolean isExportEnabled() {
            return false;
        }

        @Override
        public Type getType() {
            return Type.TYPE_A;
        }

    }

    public static class SecondChild extends AbstractChild {

        @Override
        public boolean isExportEnabled() {
            return true;
        }

        @Override
        public Type getType() {
            return Type.TYPE_B;
        }

    }

    public static class Fam {
        private int famId;

        private List<Parent> members;

        public Fam() {
            members = new ArrayList<Parent>();
        }

        public int getFamId() {
            return famId;
        }

        public void setFamId(int famId) {
            this.famId = famId;
        }

        public List<Parent> getMembers() {
            return members;
        }

        public void setMembers(List<Parent> members) {
            this.members = members;
        }

        public void addMember(Parent member) {
            members.add(member);
        }

    }

    public SimpleTestMain() {
    }

    public static void main(String[] args) {
        Fam fam = new Fam();
        FirstChild fc = new FirstChild();
        fc.setId(1);
        fc.setCode("FirstChildCode");
        fc.setMinCount(1);
        fc.setMaxCount(4);

        fam.addMember(fc);

        SecondChild sc = new SecondChild();

        sc.setCode("SecondChildCode");
        sc.setMinCount(131);
        sc.setMaxCount(431);

        fam.addMember(sc);
        String test = "";
        // Serialize it
        ObjectMapper mapper = new ObjectMapper();
        try {
            test = mapper.writeValueAsString(fam);
            System.out.println("Serialized bean:\n" + test);
            // the output
            // Serialized bean:
            // {"famId":0,"members":[{"@cls":".SimpleTestMain$FirstChild","id":1,"code":"FirstChildCode","minCount":1,"maxCount":4,"type":"TYPE_A","exportEnabled":false},{"@cls":".SimpleTestMain$SecondChild","id":0,"code":"SecondChildCode","minCount":131,"maxCount":431,"type":"TYPE_B","exportEnabled":true}]}
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Deserialize it
        mapper = new ObjectMapper();

        //      mapper.enableDefaultTyping();
        try {
            Fam fam1 = mapper.readValue(test, Fam.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
4

0 回答 0