解决Jackson反序列化错误:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
- 第一种解决方案
ObjectMapper对象添加
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String s = "{\"id\":1,\"name\":\"test\",\"age\":19}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
User value = mapper.readValue(s, User.class);
System.out.println(value);
- 第二种解决方案
在需要转化的对象的类中添加注解,注解信息如下:
@JsonIgnoreProperties(ignoreUnknown = true)
(ps:如果是maven项目,别忘了加依赖)
<!-- json到java对象的序列化和反序列化 -->
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>