Jackson 序列化/反序列化时忽略某属性

9 篇文章 0 订阅
基于Spring MVC的RESTful接口基本都使用了Jackson这个类库。

使用过程中总会有
1. 序列化时忽略某属性(如Password)
2. 反序列化时忽略某属性(如HashedPassword)

其实对应在Bean中,就是类的Setter/Getter方法。

Jackson提供了@Jsonignore这个注解,用于在(反)序列化时,忽略bean的某项属性。在Jackson 1.9的时候,@Jsonignore的语义还有了变化,如下:

1.9之前:
在Setter方法上加@Jsonignore注解并不会影响Getter方法的调用

1.9之后:
在Setter方法上加@Jsonignore会导致整个这个属性在序列化过程中被忽略。
[url]https://stackoverflow.com/questions/12505141/only-using-jsonignore-during-serialization-but-not-deserialization[/url]

所以在1.9之后需要使用其他的方法来设置某个属性是否需要(反)序列化:
@JsonProperty(access = Access.WRITE_ONLY)


通过设置JsonProperty的access属性来确定当前属性是不是需要自动序列化/反序列化。

WRITE_ONLY:仅做反序列化操作。
READ_ONLY:仅做序列化操作。

现在的问题是,2.8.7版本的jackson databind (start.spring.io在引入Spring-Web-Starter时候自动引入的,版本是Spring Boot 1.5.X-RELEASE), 在使用READ_ONLY时,并没有忽略反序列化操作,查询了一下应该是jackson databind的一个bug:
[url]https://github.com/FasterXML/jackson-databind/issues/95[/url]
[url]https://github.com/FasterXML/jackson-databind/issues/935[/url]
尽管bug已经关闭,但是似乎还是有问题,这时候有以下Work around在935中有提到:

@JsonIgnoreProperties(value="some_field", allowGetters = true, allowSetters = false)

在类上加上以上注解,工作正常。

注:并未完美解决,似乎JsonIgnoreProperties和JsonIgnore不能共存,这样的话如果某个类既有屏蔽get方法也有屏蔽set方法的话就不知道怎么搞了
另外 https://github.com/FasterXML/jackson-databind/issues/1805 是个比较新的相关bug 跟踪一下。

更新,已经查到问题,由于jackson在处理collection和map时会自动USE_GETTERS_AS_SETTERS,所以会产生问题,引用自己在github的comment:

[quote]wwwcomy commented 4 minutes ago • edited
Facing the same problem, in issue #935, seems only simple types were handled correctly.

I looked into the code, the issue was caused by some special logic for USE_GETTERS_AS_SETTERS, in BeanDeserializerFactory Line 565 (version 2.8.10):

if (propDef.hasSetter()) {
JavaType propertyType = propDef.getSetter().getParameterType(0);
prop = constructSettableProperty(ctxt, beanDesc, propDef, propertyType);
} else if (propDef.hasField()) {
JavaType propertyType = propDef.getField().getType();
prop = constructSettableProperty(ctxt, beanDesc, propDef, propertyType);
} else if (useGettersAsSetters && propDef.hasGetter()) {
/* May also need to consider getters
* for Map/Collection properties; but with lowest precedence
*/
AnnotatedMethod getter = propDef.getGetter();
// should only consider Collections and Maps, for now?
Class<?> rawPropertyType = getter.getRawType();
if (Collection.class.isAssignableFrom(rawPropertyType)
|| Map.class.isAssignableFrom(rawPropertyType)) {
prop = constructSetterlessProperty(ctxt, beanDesc, propDef);
}
}

By default USE_GETTERS_AS_SETTERS is enabled, so, although the Collection member was defined Access as "READ_ONLY", still, it is set as a property in the builder instance.

My work around is using (for spring boot applications) spring.jackson.mapper.USE_GETTERS_AS_SETTERS=false

However, I'm not sure this behavior is a bug or not, @cowtowncoder please help to clarify.[/quote]
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值