#yyds干货盘点#一个Getter引发的血案

炒沙作縻终不饱,缕冰文章费工巧。这篇文章主要讲述#yyds干货盘点#一个Getter引发的血案相关的知识,希望能为你提供帮助。
一个Getter引发的血案 需求最近做一了个需求,调用其他服务的REST接口,感觉很简单,于是迅速就搞起来了
构造Request类

public class User private String name; private Integer age; public User(String name, Integer age) this.name = name; this.age = age;

啪,我上来就一new
service.sendRequest(new User("niu", 18));

打完,收工,又是努力工作(摸鱼)的一天。
定位但是,某天晚上8点,测试人员突然给我打电话,说调用失败,同时本身又缺少打印,没有办法具体哪出问题了。
我是不会认为这么简单的代码自己会出错的,不可能!!
经过网络抓包后发现,收到的参数都是null,但是我这边明明调用构造器传入参数了
#yyds干货盘点#一个Getter引发的血案

文章图片

难道出现灵异事件了?
经过分析,整体数据流为:
#yyds干货盘点#一个Getter引发的血案

文章图片

能出现问题的地方只能是序列化JSON地方,于是本地测试验证了这一结论:
public static void main(String[] args) throws IOException ObjectMapper objectMapper = new ObjectMapper(); String request = objectMapper.writeValueAsString(new User("niu", 18)); System.out.println(request);

虽然是出问题了,但是序列化并没有转为属性为null的对象,而是直接抛出异常
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class online.jvm.bean.User and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)

通过查询异常资料,解决掉这种异常需要在增加Jackson的序列化配置FAIL_ON_EMPTY_BEANS,FAIL_ON_EMPTY_BEANS这个配置表示如果某个bean序列化为空时不会异常失败
public static void main(String[] args) throws IOException ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(FAIL_ON_EMPTY_BEANS, false); String request = objectMapper.writeValueAsString(new User("niu", 18)); System.out.println(request);

这种就不会报错,而是返回序列化成空串,也就导致接受方为属性都为null
通过看自研RPC框架看到是有该FAIL_ON_EMPTY_BEANS的配置
解决再来分析一下原因,Jackson序列化时需要调用bean的getter方法
1、写上getter后再看下结果:
public class User private String name; private Integer age; public User(String name, Integer age) this.name = name; this.age = age; public String getName() return name; public Integer getAge() return age; public static void main(String[] args) throws IOException ObjectMapper objectMapper = new ObjectMapper(); String request = objectMapper.writeValueAsString(new User("niu", 18)); System.out.println(request); // 输出正常 : "name":"niu","age":18

2、或者把属性访问权限改为public
public class User public String name; public Integer age; public User(String name, Integer age) this.name = name; this.age = age; public static void main(String[] args) throws IOException ObjectMapper objectMapper = new ObjectMapper(); String request = objectMapper.writeValueAsString(new User("niu", 18)); System.out.println(request); // 输出正常 : "name":"niu","age":18

但是如果要求不能暴露bean的属性即使是getter也不行呢?
3、注解 @JsonProperty
这是就需要使用Jackson提供的注解 @JsonProperty
public class User @JsonProperty("userName") private String name; @JsonProperty private Integer age; public User(String name, Integer age) this.name = name; this.age = age; public static void main(String[] args) throws IOException ObjectMapper objectMapper = new ObjectMapper(); String request = objectMapper.writeValueAsString(new User("niu", 18)); System.out.println(request); //"userName":"niu","age":18

来看下注解@JsonProperty的源码注释
Marker annotation that can be used to define a non-static method as a "setter" or "getter" for a logical property (depending on its signature), or non-static object field to be used (serialized, deserialized) as a logical property.

大体意思是注解如果用在属性上相当于为该属性定义getter和setter。
那如果既有getter又有@JsonProperty注解,以哪个为准呢?
public class User @JsonProperty("userName") private String name; @JsonProperty private Integer age; public User(String name, Integer age) this.name = name; this.age = age; public String getName() return name; public static void main(String[] args) throws IOException ObjectMapper objectMapper = new ObjectMapper(); String request = objectMapper.writeValueAsString(new User("niu", 18)); System.out.println(request); // "age":18,"userName":"niu"

如果getter一个没有的属性,效果如何呢?
public class User @JsonProperty("userName") private String name; @JsonProperty private Integer age; public User(String name, Integer age) this.name = name; this.age = age; public String getName2() return name; public static void main(String[] args) throws IOException ObjectMapper objectMapper = new ObjectMapper(); String request = objectMapper.writeValueAsString(new User("niu", 18)); System.out.println(request); // "age":18,"name2":"niu","userName":"niu"

这说明如果有@JsonProperty注解,先以注解为准
然后利用反射找到对象类的所有get方法,接下来去get,然后小写化,作为json的每个key值,而get方法的返回值作为value。接下来再反射field,添加到json中。
4、特殊情况
还有一种比较特殊的情况, getter方法由lombok生成,且属性的次首字母是大写:
@Getter public class User @JsonProperty private String nAme; @JsonProperty private Integer age; public User(String name, Integer age) this.nAme = name; this.age = age; public static void main(String[] args) throws IOException ObjectMapper objectMapper = new ObjectMapper(); String request = objectMapper.writeValueAsString(new User("niu", 18)); System.out.println(request); // "nAme":"niu","age":18,"name":"niu"

这是因为lombok生成的getter会把属性的第一个字母变成大写,
序列化时会把get后与小写字母中间的大写变成小写,也就是会把NA变成小写
所以序列化结果会有name(getter获取)和nAme(注解获取)两个属性
public String getNAme() return this.nAme;

如果我们自己用idea快捷键生成getter,
此时之后序列化nAme
public String getnAme() return nAme;

小结许多bug都是在自以为没有问题的地方产生,看似简单,更需要小心,同时也需要多注意序列化原理,整体感觉序列化还是用Gson更省心,完全不用关心Getter和Setter方法,会完全按照属性名来序列化。
【#yyds干货盘点#一个Getter引发的血案】本文的涉及的bug过程和解决方式希望对你也有所帮助,再见。

    推荐阅读