27

我尝试反序列化包含空属性并具有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!

问题:如何避免此异常或保证 Jackson 在序列化期间忽略空值?

抛出:

信息:

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

4 回答 4

55

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

public class Item
{
    private Float value;

    public float getValue()
    {
        return value;
    }

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

请注意 getValue() 方法的“float”而不是“Float”,这可能导致空指针异常,即使您已添加

objectMapper.setSerializationInclusion(Include.NON_NULL);
于 2015-06-04T13:01:40.520 回答
21

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

objectMapper.setSerializationInclusion(Include.NON_NULL);

希望这能解决您的问题。

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

于 2013-08-07T07:03:08.360 回答
2

我也面临同样的问题。

我只是在模型类中包含了一个默认构造函数以及另一个带参数的构造函数。

有效。

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;
}
}
于 2018-09-06T08:26:18.907 回答
0

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

@JsonProperty
private String id;
于 2019-01-30T13:44:22.983 回答