概述:
出现这个异常的原因是JSON转Java对象的时候,JSON包含有Java对象没有的属性,所以无法识别该多余的属性。解决方案有3种。
方案一:单个Class配置
在对应Class上添加注解 @JsonIgnoreProperties(ignoreUnknown = true),忽略未知属性。
代码示例:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
private final static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws JsonProcessingException {
String jsonStr = "{\"name\":\"iyushu\",\"age\":18,\"gender\":\"男\"}";
Person person = mapper.readValue(jsonStr, Person.class);
System.out.println(person);
}
}
输出结果:
Person{name='iyushu', age=18}
方案二:全局配置
使用mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 实现全局配置忽略未知属性。
代码示例:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
private final static ObjectMapper mapper = new ObjectMapper();
static {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static void main(String[] args) throws JsonProcessingException {
String jsonStr = "{\"name\":\"iyushu\",\"age\":18,\"gender\":\"男\"}";
Person person = mapper.readValue(jsonStr, Person.class);
System.out.println(person);
}
}
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
输出结果:
Person{name='iyushu', age=18}
方案三:对未知属性反序列化
使用 @JsonAnySetter 注解将JSON中多余的属性反序列化到Java POJO,
该方式需要在Java POJO 中增加一个Map的属性用来接收JSON中所有多余的属性,且对应的setter方法入参为key-value键值对。
代码示例:
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import java.util.HashMap;
import java.util.Map;
public class Person {
private String name;
private Integer age;
private Map<String, Object> other = new HashMap<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@JsonAnyGetter
public Map<String, Object> getOther() {
return other;
}
@JsonAnySetter
public void setOther(String key, Object value) {
this.other.put(key, value);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", other=" + other +
'}';
}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
private final static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws JsonProcessingException {
String jsonStr = "{\"name\":\"iyushu\",\"age\":18,\"gender\":\"男\", \"education\":\"本科\"}";
Person person = mapper.readValue(jsonStr, Person.class);
System.out.println(person);
System.out.println(mapper.writeValueAsString(person));
}
}
输出结果:
Person{name='iyushu', age=18, other={education=本科, gender=男}}
{"name":"iyushu","age":18,"education":"本科","gender":"男"}
可以看到,使用 @JsonAnySetter 注解可以将JSON中多余出来的属性反序列化到person对象的other属性中去。
同样,可以使用 @JsonAnyGetter 注解将 person对象的other属性中的kay-value键值对以平级的方式序列化到JSON对象当中,注意是和其他属性是 平级 的。