比如
public class Super {
protected String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
class Sub extends Super{
}
//在某個(gè)控制器中温亲,假如有如下方法
//假如json={"id":"1234"}
@RequestMapping("/getList")
public JSONObject get(@RequestBody Sub sub) {
String id = sub.getId();
//此時(shí)會(huì)發(fā)現(xiàn)id 為空
}
解決辦法就是,在父類中的屬性的setter方法上加注解@JsonSetter(前提是你使用jackson作為json和object的轉(zhuǎn)換工具)
Setter means that when a property with matching name is encountered in JSON content,
this method will be used to set value of the property.