Jackson反序列化抛异常:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field的解决方法
需求背景
第三方服务返回的响应报文是JSON,但是,我们只对其中的一部分感兴趣,故反序列化时创建实体无需包含JSON中的全部字段。
如反序列化"{"name":"JSON","age":"23","address":"北京市西城区","day":1633688895637}"的时候,实体类中唯独没有age字段时,系统抛出的异常如下:
Parse object to Object error
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "age" (class dto.StudentTest), not marked as ignorable (3 known properties: "name", "address", "day"])
at [Source: (String)"{"name":"JSON","age":"23","address":"北京市西城区","day":1633688895637}"; line: 1, column: 23] (through reference chain: com.cloud.isavana.media.module.report.dto.StudentTest["age"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:987)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1974)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1701)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1679)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:330)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3548)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3516)
问题分析
Jackson反序列化JSON字符串的时候,出现这种问题是由于JSON里面包含了实体没有的字段导致反序列化失败。
问题解决
使用注解@JsonIgnoreProperties
在需要转化的对象的类中添加注解@JsonIgnoreProperties(ignoreUnknown = true),示例如下:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
import java.util.Date;
/**
* @Author: Wiener
* @Date: 2021/10/8 17:02
*/
@Getter
@Setter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class StudentTest implements Serializable {
private static final long serialVersionUID = -5246589941647210011L;
//姓名
private String name;
//年龄
// private String age;
//住址
private String address;
private Date day;
}
ObjectMapper对象配置FAIL_ON_UNKNOWN_PROPERTIES
String str="{\"name\":\"JSON\",\"age\":\"29\",\"address\":\"北京市朝阳区\",\"addressTest\":\"无对应属性,不转换\"}";;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
StudentTest value = mapper.readValue(str, StudentTest.class);
System.out.println(value);
Reference
分享一句俗语:男儿断掌千斤两,女子断掌过房养。
读后有收获,小礼物走一走,请作者喝咖啡。
Buy me a coffee. ☕Get red packets.
作者:楼兰胡杨
本文版权归作者和博客园共有,欢迎转载,但请注明原文链接,并保留此段声明,否则保留追究法律责任的权利。