只使用@JsonIgnore序列化过程中,而不是反序列化 [英] Only using @JsonIgnore during serialization, but not deserialization

查看:5641
本文介绍了只使用@JsonIgnore序列化过程中,而不是反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被发送到和来自服务器的用户对象。当我送出去的用户对象我不想哈希密码发送到客户端。所以我加了 @JsonIgnore 上的密码属性,但是这也从阻断它被反序列化为这使得它很难注册用户时,他们没有得到一个密码口令

我怎么能只获得 @JsonIgnore 适用于序列化和反序列化不?我使用的春天JSONView所以我没有吨,比控制的 ObjectMapper

事情我已经试过:


  1. @JsonIgnore 添加到属性

  2. 添加 @JsonIgnore 的getter方法​​只有


解决方案

究竟如何做到这一点取决于杰克逊的版本,您正在使用。这改变周围版本的 1.9 的,在这之前,你可以通过添加 @JsonIgnore 来吸气做到这一点。

您已经试过:


  

添加@JsonIgnore的getter方法​​只有


做到这一点,的添加特定的 @JsonProperty 标注为您的JSON的密码字段名的setter方法​​输入密码的你的对象。

最近杰克逊的版本增加了 READ_ONLY WRITE_ONLY JsonProperty <标注参数/ code>。所以,你也可以这样做:

  @JsonProperty(访问= Access.WRITE_ONLY)
私人字符串密码;

文件可以发现<一个href=\"http://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonProperty.Access.html\">here.

I have a user object that is sent to and from the server. When I send out the user object I don't want to send the hashed password to the client. So I added @JsonIgnore on the password property, but this also blocks it from being deserialized into the password which makes it hard to sign up users when they ain't got a password.

How can I only get @JsonIgnore to apply to serialization and not deserialization? I'm using Spring JSONView so I don't have a ton of control over the ObjectMapper.

Things I've tried:

  1. Add @JsonIgnore to the property
  2. Add @JsonIgnore on the getter method only

解决方案

Exactly how to do this depends on the version of Jackson that you're using. This changed around version 1.9, before that, you could do this by adding @JsonIgnore to the getter.

Which you've tried:

Add @JsonIgnore on the getter method only

Do this, and also add a specific @JsonProperty annotation for your JSON "password" field name to the setter method for the password on your object.

More recent versions of Jackson have added READ_ONLY and WRITE_ONLY annotation arguments for JsonProperty. So you could also do something like:

@JsonProperty(access = Access.WRITE_ONLY)
private String password;

Docs can be found here.

这篇关于只使用@JsonIgnore序列化过程中,而不是反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆