美文网首页
注解@JsonSetter:为父类属性赋值

注解@JsonSetter:为父类属性赋值

作者: maxwellyue | 来源:发表于2017-06-21 17:34 被阅读324次

比如

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.

相关文章

网友评论

      本文标题:注解@JsonSetter:为父类属性赋值

      本文链接:https://www.haomeiwen.com/subject/tvstcxtx.html