exception while using jackson and ormlite annotations together in one object

耗尽温柔 提交于 2019-12-09 19:41:44

问题


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;
    }
}

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;
}

and my json is:

{
    "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",

        }
    ]
}

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

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


回答1:


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: https://stackoverflow.com/a/14920916/2021412.




回答2:


I have solved the issue by using Collection instead of ForeignCollection. Jackson can parse Collection now.



来源:https://stackoverflow.com/questions/15100744/exception-while-using-jackson-and-ormlite-annotations-together-in-one-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!