1. 反序列化失败,类型不匹配
View Code
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserial ize instance of java.lang.String out of START_OBJECT token at [Source: org.apache.http.conn.EofSensorInputStream@51796217; line: 1, column : 274] (through reference chain: com.expedia.lodging.domain.valueobject.poi.tip. Tip["postalAddress"]) at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingE xception.java:164) at com.fasterxml.jackson.databind.DeserializationContext.mappingExceptio n(DeserializationContext.java:691) at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deseriali ze(StringDeserializer.java:46) at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deseriali ze(StringDeserializer.java:11) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize (SettableBeanProperty.java:525)
这段错误是因为给出的filed是String类型,而序列化的是字符串对应的为{},即使一个object。
2. 不是public的filed以及没有getter
com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.test.json.jackson.User and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
当序列化的时候,即将POJO转换为json的时候,需要有个getter方法。
3. 重写了构造方法但没有生命无参构造函数
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.test.json.jackson.User]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.StringReader@5f2108b5; line: 1, column: 2]
反序列化的时候,jackson需要通过无参构造函数来创建POJO。如果这时候你添加了构造函数并且带有参数,则无参构造函数被掩盖,则报错。
4.字符串中的字段和要转换POJO的字段不匹配,如果忽略不认识的字段,需要设
@JsonIgnoreProperties(ignoreUnknown = true)
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "a" (class com.test.json.jackson.User), not marked as ignorable (one known property: "name"]) at [Source: java.io.StringReader@75329a49; line: 1, column: 9] (through reference chain: com.test.json.jackson.User["a"]) at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79) at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)