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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18096589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:21:15  来源:igfitidea点击:

JSON: JsonMappingException while try to deserialize object with null values

javajsonHymansonjson-deserialization

提问by VB_

I try to deserialize object that contains null-properties and have the JsonMappingException.

我尝试反序列化包含空属性的对象并具有JsonMappingException.

What I do:

我所做的:

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!

BUT: if to throw away "firstName = null"property - all works fine! I mean pass the next string:

但是:如果扔掉"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!

Question: How to avoid this exception or to pledge Hymanson ignore null-values during serialization?

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

Throws:

抛出:

Message:

信息:

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

cause:

原因:

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

cause:java.lang.NullPointerException

原因:java.lang.NullPointerException

采纳答案by Hymanall

If you don't want to serialize nullvalues, you can use the following setting (during serialization):

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

objectMapper.setSerializationInclusion(Include.NON_NULL);

Hope this solves your problem.

希望这能解决您的问题。

But the NullPointerExceptionyou get during deserialization seems suspicious to me (Hymanson should ideally be able to handle nullvalues in the serialized output). Could you post the code corresponding to the PersonResponseclass?

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

回答by Neman

Sometimes this problem occurs when accidentally using a primitive type as return type of the getter of a non-primitive field:

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

public class Item
{
    private Float value;

    public float getValue()
    {
        return value;
    }

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

Notice the "float" instead of "Float" for the getValue()-method, this can lead to a Null Pointer Exception, even when you have added

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

objectMapper.setSerializationInclusion(Include.NON_NULL);

回答by Abhiroop Nandi Ray

I also faced the same issue.

我也面临同样的问题。

I just included a default constructor in the model class along with the other constructor with parameters.

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

It worked.

有效。

package objmodel;

import com.fasterxml.Hymanson.databind.annotation.JsonDeserialize;
import com.fasterxml.Hymanson.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;
}
}

回答by Shankar Murthy

Add JsonProperty annotation to your attribute in TO class, as below

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

@JsonProperty
private String id;