杰克逊 - JsonMappingException由于构造函数 [英] Jackson - JsonMappingException due to constructor

查看:98
本文介绍了杰克逊 - JsonMappingException由于构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试反序列化JSON时遇到以下异常
$ b blockquote>

找不到适用于类型的合适构造函数[简单类型,类MyObj $ obj $ Card]:不能从JSON对象实例化(需要添加/启用类型信息?)at [Source:java.io.StringReader @ 4344ee21;第1行,第201列] (通过引用链: MyObj [obj] - > Obj [cards]


而JSON是

  {
obj:{
api:OK,
cache:false,
cards:[
{
id:1232995897,
items:[
{
id:vmdSJLpnY,
cat:50,
rating: 0.0
}
]
},
{
id:0005897,
items:[
{
id:vxdSJLpnY,
cat:50,
rating:0.0
}
]
}
]


Obj $ j
$ b $ p $ j $'


产生上述例外。将类型 Card [] 更改为 Object [] 不会产生异常,但它缺少正确的映射I希望得到。



任何线索我该如何解决它?一个片段将是伟大的!
这个错误意味着什么?



更新

包括Java类。

  import com.fasterxml.jackson.annotation.JsonCreator; 
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;

@JsonIgnoreProperties(ignoreUnknown = true)
public final class MyObj {
@JsonIgnoreProperties(ignoreUnknown = true)
public final class Obj {
@JsonIgnoreProperties (ignoreUnknown = true)
public final class Card {
@JsonIgnoreProperties(ignoreUnknown = true)
public final class Item {
@JsonProperty(id)private String id;
@JsonProperty(猫)private String cat;
@JsonProperty(评级)私人字符串评分;
public final String getId(){return id; }
public final String getCat(){return cat; }
public final String getRating(){return ranting; }
public final String toString(){return new Gson()。toJson(this); }
}

@JsonProperty(items)private Item [] items;
public final Item [] getItems(){return items; }
public final String toString(){return new Gson()。toJson(this); }
}

@JsonProperty(卡)私人卡[]卡;
public card [] getCards(){return cards; }
public final String toString(){return new Gson()。toJson(this); }
}


@JsonProperty(obj)MyObj obj;
public final Card [] getCards(){return apiServiceResultsNoLists.getCards(); }
}


解决方案

我认为问题是很可能是对象


  • 它可能没有默认构造函数

  • 如果它没有默认构造函数,则应该使用 @ JsonCreator

$ b 编辑 strong>
我有两件事:

*你没有setter。

*你没有一个公共构造函数可以设置你的文件。











$ b解决方案:

- >将公共setter添加到类
- >或创建用@JsonCreator注释的参数化构造函数

<当然,解析器可以做到反射式的mofidy the visibility技巧,但是,它并不是它应该被播放的方式。

EDIT2
我认为这应该可行,但我无法测试它 - 我现在没有一个与杰克逊正确设置的项目(这只是它的一部分,但我认为很容易理解我想要展示的内容)注意,我将数组更改为List:

  @JsonIgnoreProperties(ignoreUnknown = true)
public final class Card {
@JsonIgnoreProperties(ignoreUnknown = true)

public final class Item {
@JsonProperty(id )私人字符串ID;
@JsonProperty(猫)private String cat;
@JsonProperty(评级)私人字符串评分;

@JsonCreator
public Item(@JsonProperty(id)字符串id,@JsonProperty(cat)字符串cat,@JsonProperty(rating)字符串等级){
this.id = id;
this.cat = cat;
this.rating = rating;
}


public final String getId(){return id; }
public final String getCat(){return cat; }
public final String getRating(){return ranting; }
public final String toString(){return new Gson()。toJson(this); }
}
@JsonProperty(items)private List< Item>项目;

@JsonCreator
公共卡(@JsonProperty(items)List< Item> items){
this.items = items;
}

public final List< Item> getItems(){返回项目; }
$ b $ public final String toString(){return new Gson()。toJson(this); }
}


I am having the following exception when trying to deserialize a JSON

No suitable constructor found for type [simple type, class MyObj$obj$Card]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.StringReader@4344ee21; line: 1, column: 201] (through reference chain: MyObj["obj"]->Obj["cards"])

And the JSON is

{
  "obj":{
  "api":"OK",
  "cache":false,
  "cards":[
     {
        "id":1232995897,
        "items":[
           {
              "id":"vmdSJLpnY",
              "cat":50,
              "rating":0.0
           }
        ]
     },
     {
        "id":0005897,
        "items":[
           {
              "id":"vxdSJLpnY",
              "cat":50,
              "rating":0.0
           }
        ]
     }
 ]
 }
}

And within the Obj class I have the following statement

@JsonProperty("cards") private Card[] cards;

Which produces the exception above. Changing the type Card[] to Object[] does not produce an exception, but it lacks of the correct mapping I desire to get.

Any clue how can I resolve it? A snippet will be GREAT! What this error means anyhow?

UPDATE

I have included the Java class.

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;

@JsonIgnoreProperties(ignoreUnknown=true)
public final class MyObj {
    @JsonIgnoreProperties(ignoreUnknown=true)
    public final class Obj {
        @JsonIgnoreProperties(ignoreUnknown=true)
        public final class Card {
            @JsonIgnoreProperties(ignoreUnknown=true)
            public final class Item {
                @JsonProperty("id") private String id;
                @JsonProperty("cat") private String cat;
                @JsonProperty("rating") private String rating;
                public final String getId() { return id; }
                public final String getCat() { return cat; }
                public final String getRating() { return ranting; }
                public final String toString() { return new Gson().toJson(this); }
            }

            @JsonProperty("items") private Item[] items;
            public final Item[] getItems() { return items; }
            public final String toString() { return new Gson().toJson(this); }
        }

        @JsonProperty("cards") private Card[] cards;
        public Card[] getCards() { return cards; }
        public final String toString() { return new Gson().toJson(this); }      
    }


    @JsonProperty("obj") MyObj obj;
    public final Card[] getCards(){ return apiServiceResultsNoLists.getCards(); }
}

解决方案

I think the problem is most likely with the Card object

  • it might not have a default constructor
  • if it doesn't have a default constructor, it should be annotated using @JsonCreator

EDIT I have two things:
* you don't have setters.
* you don't have a public constructor that would be allowed to set your fileds.

How should the deserializer populate your fields if you don't give it any (legal*) means for it?

Solutions:
-> add public setters to the classes
-> or create parametrized constructors annotated with @JsonCreator

*: of course, the parser could do the reflective "mofidy the visibility" trick, but come on, this is not "the way it's meant to be played"

EDIT2 I think this should work, but I can't test it - I don't have a project at hand with Jackson properly set up now (this is just a part of it, but I think it is easy to interpret what I wanted to show.) Note, I changed the array to a List:

    @JsonIgnoreProperties(ignoreUnknown=true)     
public final class Card {     
    @JsonIgnoreProperties(ignoreUnknown=true)

    public final class Item {     
        @JsonProperty("id") private String id;     
        @JsonProperty("cat") private String cat;     
        @JsonProperty("rating") private String rating;     

        @JsonCreator
        public Item(@JsonProperty("id") String id, @JsonProperty("cat") String cat, @JsonProperty("rating") String rating) {
            this.id = id;
            this.cat = cat;
            this.rating = rating;
        }   


        public final String getId() { return id; }     
        public final String getCat() { return cat; }     
        public final String getRating() { return ranting; }     
        public final String toString() { return new Gson().toJson(this); }     
    }     
    @JsonProperty("items") private List<Item> items;     

    @JsonCreator
    public Card(@JsonProperty("items") List<Item> items) {
        this.items = items;
    }       

    public final List<Item> getItems() { return items; }     

    public final String toString() { return new Gson().toJson(this); }     
}     

这篇关于杰克逊 - JsonMappingException由于构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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