分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)
1.1.1. 前言
近期在使用ObjectMapper对象将json转化为对象的时候,出错,解决这个异常之后,打算分享一下,免得别人也遇到类似的问题,具体的出错信息部分如下所示:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
1.1.2. jar包
我的maven pom.xml如下所示:
<jackson-2-version>2.4.2</jackson-2-version> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </exclusion> </exclusions> <version>${jackson-2-version}</version> </dependency>
下面还原一下问题的场景。
1.1.3. bean实体
需要将json转化的实体对象User类如下:
public class User { private long id; private String name; private boolean flag; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", flag=" + flag + "]"; } }
1.1.4. 测试
具体的测试程序如下所示:
String s = "{\"id\":1,\"name\":\"test\",\"age\":19}";
ObjectMapper mapper = new ObjectMapper();
User value = mapper.readValue(s, User.class);
System.out.println(value);
因为user类中的属性有id、name、flag,而对应的s字符串中有age字段所以不匹配,报错信息如下:
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "age" (class com.daling.bpmn.test.User), not marked as ignorable (3 known properties: "id", "name", "flag"])
at [Source: {"id":1,"name":"test","age":19}; line: 1, column: 31] (through reference chain: com.daling.bpmn.test.User["age"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:731)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:915)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1298)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:243)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2146)
at com.daling.vrm.controller.product.NewShelvesController.main(NewShelvesController.java:71)
1.1.5. 最终解决方案
1.1.5.1. 第一种解决方案
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);
1.1.5.2. 第二种解决方案
在需要转化的对象的类中添加注解,注解信息如下:
@JsonIgnoreProperties(ignoreUnknown = true)
public class User
...
分享牛,分享、我们是快乐的。