除了在使用杰克逊和ormlite注释一起在一个对象 [英] exception while using jackson and ormlite annotations together in one object

查看:341
本文介绍了除了在使用杰克逊和ormlite注释一起在一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的杰克逊库解析成JSON对象,并使用 ormlite 存储相同对象的sqlite的分贝。这是我的模型类:

I am using Jackson library to parse json into object and using ormlite to store same objects to the sqlite db. Here is my model classes:

public class Site {
    private String uniqueId;
    private String name;
    private ForeignCollection<ContactDetails> items;

    @JsonProperty("contact_details")
    public void setContactDetails(ForeignCollection<ContactDetails> contact_details) {
        this.items = contact_details;
    }

    public List<ContactDetails> getContactDetails() {
        return new ArrayList<ContactDetails>(items);
    }

    public String getUniqueId() {
        return uniqueId;
    }
    @JsonProperty("unique_id")
    public void setUniqueId(String uniqueId) {
        this.uniqueId = uniqueId;
    }
    public String getName() {
        return name;
    }
    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }
}

和ContactDetails类是:

and the ContactDetails class is:

public class ContactDetails {

    @JsonProperty("contact_detail_id")
    int getContactDetailId;
    @JsonProperty("cellphone_number")
    String getCellphoneNumber;
    @JsonProperty("email")
    String getEmail;
    @JsonProperty("name")
    String getName;
}

和我的JSON是:

{
    "unique_id": "WDV000282",
    "name": "2XL - Diverse werken - Zeebrugge",

    "contact_details": [
        {
            "contact_detail_id": 20647,
            "cellphone_number": "123456",
            "email": "plabon@gmail.com",
            "name": "plabon",

        },
        {
            "contact_detail_id": 20648,
            "cellphone_number": "",
            "email": "modak@gmail.com",
            "name": "test",

        }
    ]
}

但是,当我执行readvalue:

But when i execute readvalue:

Site test= objectMapper.readValue(json, Site.class); 

我得到以下异常

i get following exception

org.codehaus.jackson.map.JsonMappingException: Can not find a deserializer for non-concrete Collection type [collection type; class com.j256.ormlite.dao.ForeignCollection, contains [simple type, class com.example.jacksonparsingtest.ContactDetails]]

我没有得到什么正在发生的事情?? plz帮助...

I am not getting what is happening??Plz help...

推荐答案

由于ForeignCollection是一个接口,杰克逊不能实例化类型的新对象。我会尝试任何注释与 @JsonDeserialize领域(如= ConcreteSubclassOfForeignCollection.class) ,用一个具体的子类 BaseForeignCollection ,或使用一个简单的列表像这样的解决方案:<一href="http://stackoverflow.com/a/14920916/2021412">http://stackoverflow.com/a/14920916/2021412.

Since ForeignCollection is an interface, Jackson cannot instantiate a new Object of that kind. I would try to either annotate the field with @JsonDeserialize(as=ConcreteSubclassOfForeignCollection.class), use a concrete subclass like BaseForeignCollection or use a simple list like in this solution: http://stackoverflow.com/a/14920916/2021412.

这篇关于除了在使用杰克逊和ormlite注释一起在一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆