首页 新闻 会员 周边

jackson json @JsonIgnore问题

0
悬赏园豆:20 [已解决问题] 解决于 2017-08-02 14:16

我现在正使用springmvc开发restful风格的接口。

在响应某个user对象时,我不想把密码字段(password)也响应出去,于是在user的getPassword方法上加了@JsonIgnore注解,这样password字段就被忽略了。

但问题就来了,我在使用@RequestBody接收json格式的user对象时,jackson 同样把password字段忽略了,但这显然不是我想要的。

按照“正常”的逻辑,我只在getter方法上加了注解而没有在setter方法上加注解,jackson理应设置password。

请问有谁知道怎么解决这个问题吗,谢谢!!!

学数学的程序猿的主页 学数学的程序猿 | 菜鸟二级 | 园豆:252
提问于:2017-08-01 10:47
< >
分享
最佳答案
0

在setter方法上加上@JsonProperty注解.

具体原因可参考@JsonIgnore注解源码中的一部分注释:
 * In addition, starting with Jackson 1.9, if this is the only annotation
 * associated with a property, it will also cause cause the whole
 * property to be ignored: that is, if setter has this annotation and
 * getter has no annotations, getter is also effectively ignored.
 * It is still possible for different accessors to use different
 * annotations; so if only "getter" is to be ignored, other accessors
 * (setter or field) would need explicit annotation to prevent
 * ignoral (usually {@link JsonProperty}).

收获园豆:20
湛泸 | 菜鸟二级 |园豆:239 | 2017-08-02 12:53

确实是这样,没注意看文档……Thx!!!

学数学的程序猿 | 园豆:252 (菜鸟二级) | 2017-08-02 14:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册