比如
public class Super {
protected String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
class Sub extends Super{
}
//在某个控制器中,假如有如下方法
//假如json={"id":"1234"}
@RequestMapping("/getList")
public JSONObject get(@RequestBody Sub sub) {
String id = sub.getId();
//此时会发现id 为空
}
解决办法就是,在父类中的属性的setter方法上加注解@JsonSetter(前提是你使用jackson作为json和object的转换工具)
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.
网友评论