我现在正使用springmvc开发restful风格的接口。
在响应某个user对象时,我不想把密码字段(password)也响应出去,于是在user的getPassword方法上加了@JsonIgnore注解,这样password字段就被忽略了。
但问题就来了,我在使用@RequestBody接收json格式的user对象时,jackson 同样把password字段忽略了,但这显然不是我想要的。
按照“正常”的逻辑,我只在getter方法上加了注解而没有在setter方法上加注解,jackson理应设置password。
请问有谁知道怎么解决这个问题吗,谢谢!!!
在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}).
确实是这样,没注意看文档……Thx!!!