仅在序列化过程中使用@JsonIgnore,而不是反序列化
本文翻译自:Only using @JsonIgnore during serialization, but not deserialization
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 that makes it hard to sign up users when they ain't got a password. 因此,我在password属性上添加了@JsonIgnore
,但这也阻止了将其反序列化为密码,这使得在没有密码的情况下很难注册用户。
How can I only get @JsonIgnore
to apply to serialization and not deserialization? 我怎样才能只将@JsonIgnore
应用于序列化而不是反序列化? I'm using Spring JSONView, so I don't have a ton of control over the ObjectMapper
. 我正在使用Spring JSONView,所以我对ObjectMapper
的控制不多。
Things I've tried: 我尝试过的事情:
- Add
@JsonIgnore
to the property 将@JsonIgnore
添加到属性 - Add
@JsonIgnore
on the getter method only@JsonIgnore
在getter方法上添加@JsonIgnore
#1楼
参考:https://stackoom.com/question/qT9p/仅在序列化过程中使用-JsonIgnore-而不是反序列化
#2楼
Exactly how to do this depends on the version of Jackson that you're using. 确切的操作方法取决于您使用的Jackson版本。 This changed around version 1.9 , before that, you could do this by adding @JsonIgnore
to the getter. 这在1.9版之前发生了变化,在此之前,您可以通过在getter中添加@JsonIgnore
来实现。
Which you've tried: 您尝试过的:
Add @JsonIgnore on the getter method only 仅在getter方法上添加@JsonIgnore
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. 执行此操作, 然后将JSON“密码”字段名称的特定@JsonProperty
批注添加到对象密码的setter方法中。
More recent versions of Jackson have added READ_ONLY
and WRITE_ONLY
annotation arguments for JsonProperty
. 最近杰克逊的版本增加了READ_ONLY
和WRITE_ONLY
注释论据JsonProperty
。 So you could also do something like: 因此,您还可以执行以下操作:
@JsonProperty(access = Access.WRITE_ONLY)
private String password;
Docs can be found here . 可以在这里找到文档。
#3楼
In order to accomplish this, all that we need is two annotations: 为了做到这一点,我们需要的是两个注释:
@JsonIgnore
@JsonProperty
Use @JsonIgnore
on the class member and its getter, and @JsonProperty
on its setter. 使用@JsonIgnore
类成员和其吸气剂,并在@JsonProperty
它的制定者。 A sample illustration would help to do this: 示例示例将有助于实现此目的:
class User {// More fields here@JsonIgnoreprivate String password;@JsonIgnorepublic String getPassword() {return password;}@JsonPropertypublic void setPassword(final String password) {this.password = password;}
}
#4楼
In my case, I have Jackson automatically (de)serializing objects that I return from a Spring MVC controller (I am using @RestController with Spring 4.1.6). 就我而言,我让Jackson从Spring MVC控制器返回的对象自动(反序列化)对象(我在Spring 4.1.6中使用@RestController)。 I had to use com.fasterxml.jackson.annotation.JsonIgnore
instead of org.codehaus.jackson.annotate.JsonIgnore
, as otherwise, it simply did nothing. 我不得不使用com.fasterxml.jackson.annotation.JsonIgnore
而不是org.codehaus.jackson.annotate.JsonIgnore
,否则,它什么也没做。
#5楼
Since version 2.6: a more intuitive way is to use the com.fasterxml.jackson.annotation.JsonProperty
annotation on the field: 从2.6版开始:一种更直观的方法是在字段上使用com.fasterxml.jackson.annotation.JsonProperty
批注:
@JsonProperty(access = Access.WRITE_ONLY)
private String myField;
Even if a getter exists, the field value is excluded from serialization. 即使存在吸气剂,该字段值也会从序列化中排除。
JavaDoc says: JavaDoc说:
/*** Access setting that means that the property may only be written (set)* for deserialization,* but will not be read (get) on serialization, that is, the value of the property* is not included in serialization.*/
WRITE_ONLY
In case you need it the other way around, just use Access.READ_ONLY
. 万一您需要它,只需使用Access.READ_ONLY
。
#6楼
"user": {"firstName": "Musa","lastName": "Aliyev","email": "klaudi2012@gmail.com","passwordIn": "98989898", (or encoded version in front if we not using https)"country": "Azeribaijan","phone": "+994707702747"}
@CrossOrigin(methods=RequestMethod.POST)
@RequestMapping("/public/register")
public @ResponseBody MsgKit registerNewUsert(@RequestBody User u){root.registerUser(u);return new MsgKit("registered");
}
@Service
@Transactional
public class RootBsn {@Autowired UserRepository userRepo;public void registerUser(User u) throws Exception{u.setPassword(u.getPasswordIn());//Generate some salt and setPassword (encoded - salt+password)User u=userRepo.save(u);System.out.println("Registration information saved");}}
@Entity
@JsonIgnoreProperties({"recordDate","modificationDate","status","createdBy","modifiedBy","salt","password"})public class User implements Serializable {private static final long serialVersionUID = 1L;@Id@GeneratedValue(strategy=GenerationType.AUTO)private Long id;private String country;@Column(name="CREATED_BY")private String createdBy;private String email;@Column(name="FIRST_NAME")private String firstName;@Column(name="LAST_LOGIN_DATE")private Timestamp lastLoginDate;@Column(name="LAST_NAME")private String lastName;@Column(name="MODIFICATION_DATE")private Timestamp modificationDate;@Column(name="MODIFIED_BY")private String modifiedBy;private String password;@Transientprivate String passwordIn;private String phone;@Column(name="RECORD_DATE")private Timestamp recordDate;private String salt;private String status;@Column(name="USER_STATUS")private String userStatus;public User() {}// getters and setters}
仅在序列化过程中使用@JsonIgnore,而不是反序列化相关推荐
- xml Android 冒号,冒号字符在XML元素序列化过程中被编码为x003A
在实例化对象并使用XmlSerializer进行序列化之后,我将类型定义为Example,如下所示,我得到的是x003A而不是冒号: 这是我的代码: public class Example { [X ...
- django序列化器嵌套_Django Rest Framework中用于OneToOne字段的序列化程序中的嵌套关系
django序列化器嵌套 The Django Rest Framework (DRF) is one of the effectively written frameworks around Dja ...
- 制药行业验证过程中的偏差如何处理?
制药企业进行验证活动时--可能会出现测试结果与验证目标不一致的情况,对于同一个不一致,不同的原因处理方式亦不同.对偏差进行分类管理,以不同的方法和流程区分对待,一方面能达到降低质量风险的目的,另一方面 ...
- r语言 rgl 强制过程中_一个R语言中操纵矢量空间数据的标准化工具—sf
注: 本文是R语言sf包的核心开发者和维护者--来自德国明斯特大学的地理信息学教授:Edzer Pebesma 的一篇关于sf包的简介,发表于2018年7月的R语言期刊,主要讲述了sf的定位.功能. ...
- Hadoop-2.8.0集群搭建、hadoop源码编译和安装、host配置、ssh免密登录、hadoop配置文件中的参数配置参数总结、hadoop集群测试,安装过程中的常见错误
25. 集群搭建 25.1 HADOOP集群搭建 25.1.1集群简介 HADOOP集群具体来说包含两个集群:HDFS集群和YARN集群,两者逻辑上分离,但物理上常在一起 HDFS集群: 负责海量数据 ...
- dqn在训练过程中loss越来越大_DQN算法实现注意事项及排错方法
在学习强化学习过程中,自己实现DQN算法时,遇到了比较多的问题,花了好几天的时间才得以解决.最后分析总结一下,避免再走弯路. 有可能开始实现出来的DQN算法,无论怎么训练总是看不错成果.需要注意的地方 ...
- java get方法不序列化_Java中的Json序列化,不容忽视的getter
在开发的过程中,经常会碰到和自己预期不一样的情况.有的时候自己去研究一下还是很有趣的.这两天在写java web的时候,碰到了一个对象序列化的问题. 问题重现 public class AjaxJso ...
- 对象序列化Java中的序列化
首先声明,我是一个菜鸟.一下文章中出现技术误导情况盖不负责 当两个进程在停止远程通信时,彼此可以发送各种类型的数据.无论是何种类型的数据,都市以二进制序列的情势在络网上传送.发送方需要把这个Java对 ...
- 张雪峰:创业团队极速发展过程中的分分合合
云栖君导读:2018第二届研发效能嘉年华专场,云效邀请了饿了么首席技术官张雪峰带来主题为<创业团队急速发展过程中的分分合合>的演讲.本次演讲主要从三个方面进行讲述,首先介绍了著名的康威定律 ...
最新文章
- spring mvc velocity 配置备忘
- 进击的python【第一集】
- php-fpm添加service服务
- 机器学习 不均衡数据的处理方法
- TZOJ 2569 Wooden Fence(凸包求周长)
- Struts2自定义拦截器实例—登陆权限验证
- leetcode 434. 字符串中的单词数(Java版)
- 谷歌微软高通反对英伟达收购ARM 值得国人深思
- 【渝粤教育】广东开放大学 数据采集技术 形成性考核 (29)
- 依赖注入原理 php,PHP依赖注入原理与用法分析
- 如何在C#里实现端口监视呢?
- Unity的Animator中Transition有延迟的问题
- mysql盲注ascii_[翻译]关于通过对8bit的ascii做右位移提高mysql盲注效率
- 一道微软面试题的Java解法
- 《转》通往高级工程师的道路
- 数据库优化-水平拆分 垂直拆分
- LCD1602----LiquidCrystal库的使用2
- Maxon Motor参数查询方法
- 压缩文件密码解密工具介绍
- 服务器上的文件夹访问被拒绝,修改windows文件权限,解决“拒绝访问”或无法删除的问题-文件访问被拒绝...