JSON:JsonMappingException同时尝试使用空值反序列化对象

时间:2013-08-07 06:55:45

标签: java json jackson json-deserialization

我尝试反序列化包含null-properties的对象并拥有JsonMappingException

我做了什么:

String actual = "{\"@class\" : \"PersonResponse\"," +
                "  \"id\" : \"PersonResponse\"," +
                "  \"result\" : \"Ok\"," +
                "  \"message\" : \"Send new person object to the client\"," +
                "  \"person\" : {" +
                "    \"id\" : 51," +
                "    \"firstName\" : null}}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(new StringReader(json), PersonResponse.class); //EXCEPTION!

但是:如果扔掉"firstName = null"财产 - 一切正常! 我的意思是传递下一个字符串:

String test = "{\"@class\" : \"PersonResponse\"," +
                "  \"id\" : \"PersonResponse\"," +
                "  \"result\" : \"Ok\"," +
                "  \"message\" : \"Send new person object to the client\"," +
                "  \"person\" : {" +
                "    \"id\" : 51}}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(new StringReader(json), PersonResponse.class); //ALL WORKS FINE!

问题: 如何避免此异常或承诺杰克逊在序列化期间忽略空值?

抛出:

消息:

com.fasterxml.jackson.databind.MessageJsonException:
 com.fasterxml.jackson.databind.JsonMappingException:
  N/A (through reference chain: person.Create["person"]->Person["firstName"])

原因:

com.fasterxml.jackson.databind.MessageJsonException:
 com.fasterxml.jackson.databind.JsonMappingException:
  N/A (through reference chain: prson.Create["person"]->Person["firstName"])

原因: java.lang.NullPointerException

4 个答案:

答案 0 :(得分:48)

有时在意外使用基本类型作为非原始字段的getter的返回类型时会出现此问题:

public class Item
{
    private Float value;

    public float getValue()
    {
        return value;
    }

    public void setValue(Float value)
    {
        this.value = value;
    }   
}

注意"浮动"而不是" Float"对于getValue() - 方法,这可能会导致空指针异常,即使您已添加

objectMapper.setSerializationInclusion(Include.NON_NULL);

答案 1 :(得分:18)

如果您不想序列化null值,可以使用以下设置(序列化期间):

objectMapper.setSerializationInclusion(Include.NON_NULL);

希望这能解决你的问题。

但是反序列化期间得到的NullPointerException对我来说似乎很可疑(杰克逊理想情况下应该能够处理序列化输出中的null值)。你能发布与PersonResponse类相对应的代码吗?

答案 2 :(得分:0)

我也面临着同样的问题。

我只是在模型类中包括一个默认构造函数,以及其他带有参数的构造函数。

有效。

package objmodel;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

public class CarModel {

private String company;
private String model;
private String color;
private String power;


public CarModel() {
}

public CarModel(String company, String model, String color, String power) {
    this.company = company;
    this.model = model;
    this.color = color;
    this.power = power;

}

@JsonDeserialize
public String getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company = company;
}

@JsonDeserialize
public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

@JsonDeserialize
public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

@JsonDeserialize
public String getPower() {
    return power;
}

public void setPower(String power) {
    this.power = power;
}
}

答案 3 :(得分:0)

将JsonProperty批注添加到TO类的属性中,如下所示

@JsonProperty
private String id;