Source code before, no secrets.


Hi, I’m the Java class representative. For more information about Java, please follow the official account:
The Java class representative.

“Ali development specification taishan edition (2020.04.22) — — — — > > programming code (a) naming style – > stipulated in article 8:

Do not prefix any Boolean variables in POJO classes with IS, or partial framework parsing will cause serialization errors.

For such a “mandatory” level of regulation, although the specification has made a simple explanation, but it is still very insignificant, so that although I know the specification very well, I still stepped into this pit.

0 causes

Recently wrote a pin warning tool class, for this kind of requirements clear, the development of clear documentation of the task, write the code is easy to finish, quickly.

However, during self-testing, I found that the @owner interface never passed the unit test. After some tracking, I finally found the reason, so I came up with this article.

1. Problem recurrence

The message itself is a post request, and nail Ali endorsement, call impassability that nature is my developer’s problem.

The following is the interface description of the pin. The isAtAll parameter indicates whether @ is the owner

{"msgtype": "text", "text": {"content": "I am not the same as I am @156xxxx8827"}, "at": {"atMobiles": [ "156xxxx8827", "189xxxx8325" ], "isAtAll": false } }

Based on this JSON string, I wrote the following POJO class to correspond to it and use FastJSON for JSON serialization

Public class Message {/** * Message type **/ private String MsgType = "Text "; /** */ private Text (); /** * @Object **/ private At At; Public static class At{/** * @ @ private List<String>; Private Boolean isAtAll; private Boolean isAtAll; private Boolean isAtAll; // Omit getter, setter} // Omit irrelevant code... }

Private Boolean isAtAll; private Boolean isAtAll; In this field, have you found that this parameter of Ali violates the development specification mentioned at the beginning? With the FastJSON serialization, the property is actually converted to:

"atAll": true

What a fool! He ate the IS in front! I can’t @ everyone

2. Go back to the roots

Why eat IS after serialization? With this problem in mind, I traced the FastJSON source code and found that when serialized, it uses the getter method name of the property, while the getter and setter automatically generated by the isAtAll field are:

public boolean isAtAll() {
    return isAtAll;
}

public void setAtAll(boolean atAll) {
    isAtAll = atAll;
 }

The corresponding fastjson treatment in com.alibaba.fastjson.util.TypeUtils.com puteGetters method names in the source code excerpt is as follows:

if(methodName.startsWith("is")){ if(methodName.length() < 3){ continue; } if(method.getReturnType() ! = Boolean.TYPE && method.getReturnType() ! = Boolean.class){ continue; } String propertyName; Field field = null; if(Character.isUpperCase(c2)){ if(compatibleWithJavaBean){ propertyName = decapitalize(methodName.substring(2)); } else{ propertyName = Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3); } propertyName = getPropertyNameByCompatibleFieldName(fieldCacheMap, methodName, propertyName, 2); }

That is, for methods that begin with IS, FastJSON truncates the third character, converts it to lowercase if it is uppercase, and marshals the rest of the method name to form the property name.

For example, the isAtAll method assembles an attribute named atAll, and the end result is that we eat our is!

3 Solutions

Now that the root cause of the problem has been found, we just need to apply the medicine to the case. Here, the course representative summarizes three solutions for different application scenarios:

  1. For code that has permission to modify, strictly follow the development specification and do not use Boolean properties in POJO classesisName the beginning, if any, change it
  2. For third party interfaces, the parameters are similarisXXXYou can use the FastJSON parameter on the corresponding property field@JSONField(name = "anotherName")To customize the property name
  3. You can manually change the getter and setter method names

4 extended

SpringBoot is integrated with Jackson, and the default is to use Jackson for JSON serialization. After testing, Jackson can also eat IS, which is similar to fastJSON

reference

  • Fastjson source
  • Ali Development Standards Taishan Edition (2020.04.22)

In this tutorial, RabbitMQ will use Spring Validation to validate parameters in a more efficient manner. The template development manual will explore the Dubbo exception handling source code and its best practices. It’s time you read the RFC documentation! MySQL priority queue (order by limit problem)


Code word is not easy, welcome thumb up concern and share.

Search: [JAVA class representative], pay attention to the public number, daily one watch, timely access to more JAVA dry goods.