Java JSON:JsonMappingException,同时尝试使用空值反序列化对象
我尝试反序列化包含null属性且具有Java JSON:JsonMappingException,同时尝试使用空值反序列化对象,java,json,jackson,json-deserialization,Java,Json,Jackson,Json Deserialization,我尝试反序列化包含null属性且具有JsonMappingException的对象 我的工作: String actual = "{\"@class\" : \"PersonResponse\"," + " \"id\" : \"PersonResponse\"," + " \"result\" : \"Ok\"," + " \"message\" : \"Send new person obj
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!
问题:
如何避免此异常或在序列化期间忽略空值
抛出:
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!
消息:
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"])
原因:
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
如果不想序列化null
值,可以使用以下设置(在序列化期间):
objectMapper.setSerializationInclusion(Include.NON_NULL);
希望这能解决你的问题
但是在反序列化过程中得到的
NullPointerException
对我来说似乎是可疑的(Jackson最好能够处理序列化输出中的null
值)。您可以发布与PersonResponse
类相对应的代码吗?有时,当意外地将基元类型用作非基元字段的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);
我也面临同样的问题 我只是在模型类中包含了一个默认构造函数以及另一个带参数的构造函数 成功了
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;
}
}
将JsonProperty注释添加到to类中的属性中,如下所示
@JsonProperty
private String id;
哈。我的person类中也有集合,因此我需要将Include.NOT_设置为空。谢谢我应该把objectMapper放在哪里?在控制器内?还是在波乔豆里?谢谢……它不是空的,不是空的。请注意,这是因为
getValue()
中的隐式强制,它基本上是执行“return value.floatValue()”来获取原语——因此是NPE。调用者无法避免这一点,或者知道存在这样的问题(除非它去解码字节码等)。您先生救了我一天:)我有一个布尔值,但在getter中意外地将其作为布尔值,它是空的:这就是它。我还有一个Boolean
和一个Boolean get…()
方法。我的问题是getter/setter有一个输入错误。在我使用Eclipse的工具重新创建了getter和setter之后,您就是救世主。我调试和分析它就像一个大问题,这是我方面的一个简单错误。非常感谢:)我还尝试删除注释“@jsondeselization”,并删除行objectmapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);它工作得很好!!