【问题标题】:exception while using jackson and ormlite annotations together in one object在一个对象中同时使用 jackson 和 ormlite 注释时出现异常
【发布时间】:2013-02-26 22:35:05
【问题描述】:

我正在使用 Jackson 库将 json 解析为对象,并使用 ormlite 将相同的对象存储到 sqlite db。这是我的模型类:

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 类是:

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 时:

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

我得到以下异常

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

我不知道发生了什么??请帮助...

【问题讨论】:

    标签: android json jackson deserialization ormlite


    【解决方案1】:

    由于 ForeignCollection 是一个接口,Jackson 无法实例化这种新对象。我会尝试使用@JsonDeserialize(as=ConcreteSubclassOfForeignCollection.class) 注释该字段,使用BaseForeignCollection 之类的具体子类或使用此解决方案中的简单列表:https://stackoverflow.com/a/14920916/2021412

    【讨论】:

      【解决方案2】:

      我已经通过使用 Collection 而不是 ForeignCollection 解决了这个问题。 Jackson 现在可以解析 Collection 了。

      【讨论】:

      • 这基本上是我回答的第三个建议……
      • 是的,我现在已经看到了。谢谢我接受你的回答。撤回您的反对票。
      • 我在将这些数据插入数据库时​​遇到了另一个问题。我为此打开了一个单独的线程。你能帮我么。 here is the link
      • 无法撤消投票(直到进行编辑),但会为(过度)补偿的问题投票。
      猜你喜欢
      • 2016-07-02
      • 2017-04-16
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 2015-04-05
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多