30

Java变量命名前俩个字母仅含有一个大写字母的坑

 3 years ago
source link: http://www.cnblogs.com/fourther/p/13796764.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

背景

前几周在做项目 fetch 切换,即将 HttpUtils 调用改成使用 Feign 调用。大概代码如下:

// 原代码
String resultJson = HttpUtil.get(url + "/fin/test?code=" + code, null);
RespDTO<Result> respDTO = JSON.parseObject(resultJson, new TypeReference<RespDTO<Result>>() {});

// 现代码如下
RespDTO<Result> respDTO = urlClient.getTest(code);

代码上线后,出现了异常。表现为: respDTO 的某个字段为 null ,但是第三方是有传过来这个值的。

问题复现

public static void main(String[] args) throws JsonProcessingException, IntrospectionException {
	String data = "{\"aFiled\":10,\"normalFiled\":20}";

  // 原方式
	Domain domain = JSON.parseObject(data, Domain.class);
	System.out.println("domain = " + domain.getAFiled());

	// feign方式
	ObjectMapper mapper = new ObjectMapper();
	Domain domain1 = mapper.readValue(data, Domain.class);
	System.out.println("domain1 = " + domain1.getAFiled());
}

https://github.com/wangjie-fourth/jackson01

问题分析

既然请求返回数据,但接收对象没有对应的值,那就说明字符串没有反序列化到指定的变量名上。改之前是使用 FastJson 反序列化数据,改之后的 Feign 默认是采用 Jackson 反序列化数据。那么为什么 FastJson 可以反序列化 aFiledJackson 不可以呢?

FastJson 是根据变量名称来反序列化的,也就是说它接收到 aFiled 数据,就会到对象找 aFiled 变量名,然后附上值;而 Jackson 默认是根据 JavaBean 规范找到对应属性后再赋值,也就是说 Jackson 并没有在这个对象找到 aFiled 属性。

JavaBean 属性名称跟变量名称是不一定相同的。 JavaBean 是通过对象的 getset 方法来确定对象属性,其属性名称是由其对象变量来决定的,通常的逻辑是:将属性首字母转成大写。但也有例外就是,前俩个字母都是大写的情况:

8.8 Capitalization of inferred names.  When we use design patterns to infer a property or event name, we need to decide what rulesto follow for capitalizing the inferred name. If we extract the name from the middle of a normalmixedCase style Java name then the name will, by default, begin with a capital letter.  Java programmers are accustomed to having normal identifiers start with lower case letters.Vigorous reviewer input has convinced us that we should follow this same conventional rulefor property and event names.  Thus when we extract a property or event name from the middle of an existing Java name, wenormally convert the first character to lower case. However to support the occasional use of allupper-case names, we check if the first two characters of the name are both upper case and ifso leave it alone. So for example,  “FooBah” becomes “fooBah”  “Z” becomes “z”  “URL” becomes “URL”  We provide a method Introspector.decapitalize which implements this conversion rule.

Jackson 根据 getset 方法来确定属性的名称。而变量前俩个字母含有一个大写字母对应的属性名称会很怪。如下:

ay6fEz6.png!mobile

我们项目上使用的是 lombok ,其生成的 getset 方法是不遵循 JavaBean 规范的,只是将变量名的首字母大写而已,所以它生成 aFiled 的方法是 getAFiled

所以, Jackson 在接收到 aFiled 属性值,它会到对象找 setaFiled 方法,自然这个对象是没有这个方法的,所以就没映射到 aFiled 字段上。

解决办法

jackson 可以使用 @JsonProperty 注解来指定属性名称。

总结

出现这个就是因为 JavaBean 规范对前俩个字母含有大写字母的变量名做了特殊处理。 Jackson 遵循 JavaBean 规范来反序列化,而项目使用的 Lombok 插件是不遵循 JavaBean 规范。

3QNjqi2.png!mobile

扩展

JavaBean 规范对前俩个字母含有大写字母的处理不全

针对变量名前俩个字母做个穷举,就是如下对象:

public class Domain {
    private Integer aFiled;
    private Integer afiled;
    private Integer AFiled;
    private Integer Afiled;

    public Integer getaFiled() {
        return aFiled;
    }

    public void setaFiled(Integer aFiled) {
        this.aFiled = aFiled;
    }

    public Integer getAfiled() {
        return afiled;
    }

    public void setAfiled(Integer afiled) {
        this.afiled = afiled;
    }

    public Integer getAFiled() {
        return AFiled;
    }

    public void setAFiled(Integer AFiled) {
        this.AFiled = AFiled;
    }
}

你会发现 afiledAfiled 生成的属性是一致的!不过好在项目中没有人只将首字母大写这种命名风格。

谨慎用 Lombok 替换旧项目的 getset 方法

旧项目一般都是用 Idea 来生成 getset 方法,而 Idea 是遵循 JavaBean 规范来生成属性的。所以,旧项目如果含有前俩个仅含有一个大写字母时,尽量不用药 lombok 去替换。

参考链接


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK