オンラインインシデントレビュー
以前、非常にシンプルな機能が追加されました.夜にオンラインになったときにreview
、会社の勤勉で進取の気性に富んだ価値観を考えて、一時的にログログを追加しました.単純なログで基本的に問題はないと感じました.コードをロールバックして問題を発見し、ログを追加したコードを削除して、再びオンラインにしました。
シナリオ復元
を定義します
CountryDTO
public class CountryDTO {
private String country;
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return this.country;
}
public Boolean isChinaName() {
return this.country.equals("中国");
}
}
复制代码
テストクラスを定義する
FastJonTest
public class FastJonTest {
@Test
public void testSerialize() {
CountryDTO countryDTO = new CountryDTO();
String str = JSON.toJSONString(countryDTO);
System.out.println(str);
}
}
复制代码
ランタイム空指针
エラー:エラー メッセージから、シリアライゼーション プロセス中にisChinaName()
メソッド。このとき、this.country
変数は空です。問題は次のとおりです。
- シリアル化はなぜ行われるの
isChinaName()
ですか? - ひいては、シリアル化プロセス中に実行されるメソッドは何ですか?
ソースコード分析
デバッグを通じてコールチェーンのスタック情報を観察する コールチェーンでは、テクノロジーを使用してクラスASMSerializer_1_CountryDTO.write
が動的に生成されます。FastJson
asm
ASMSerializer_1_CountryDTO
asm テクノロジの使用シナリオの 1 つは、
java
動的にクラスを生成してリフレクションを置き換え、繰り返し実行中のリフレクションのオーバーヘッドを回避することです。
JavaBeanSerizlier シリアライゼーションの原則
以下の図からわかるように、シリアル化の過程では、主JavaBeanSerializer
にクラスのメソッドが呼び出されwrite()
ます。そしてJavaBeanSerializer
、主にgetObjectWriter()
メソッドされgetObjectWriter()
、実行プロセスをデバッグすることにより、より重要なcom.alibaba.fastjson.serializer.SerializeConfig#createJavaBeanSerializer
メソッドを見つけてから見つけますcom.alibaba.fastjson.util.TypeUtils#computeGetters
public static List<FieldInfo> computeGetters(Class<?> clazz, //
JSONType jsonType, //
Map<String,String> aliasMap, //
Map<String,Field> fieldCacheMap, //
boolean sorted, //
PropertyNamingStrategy propertyNamingStrategy //
){
//省略部分代码....
Method[] methods = clazz.getMethods();
for(Method method : methods){
//省略部分代码...
if(method.getReturnType().equals(Void.TYPE)){
continue;
}
if(method.getParameterTypes().length != 0){
continue;
}
//省略部分代码...
JSONField annotation = TypeUtils.getAnnotation(method, JSONField.class);
//省略部分代码...
if(annotation != null){
if(!annotation.serialize()){
continue;
}
if(annotation.name().length() != 0){
//省略部分代码...
}
}
if(methodName.startsWith("get")){
//省略部分代码...
}
if(methodName.startsWith("is")){
//省略部分代码...
}
}
}
复制代码
コードから、大まかに次の 3 つのケースに分けることができます。
@JSONField(.serialize = false, name = "xxx")
注釈getXxx()
: get で始まるメソッドisXxx()
: is で始まるメソッド
シリアル化のフローチャート
サンプルコード
/**
* case1: @JSONField(serialize = false)
* case2: getXxx()返回值为void
* case3: isXxx()返回值不等于布尔类型
* case4: @JSONType(ignores = "xxx")
*/
@JSONType(ignores = "otherName")
public class CountryDTO {
private String country;
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return this.country;
}
public static void queryCountryList() {
System.out.println("queryCountryList()执行!!");
}
public Boolean isChinaName() {
System.out.println("isChinaName()执行!!");
return true;
}
public String getEnglishName() {
System.out.println("getEnglishName()执行!!");
return "lucy";
}
public String getOtherName() {
System.out.println("getOtherName()执行!!");
return "lucy";
}
/**
* case1: @JSONField(serialize = false)
*/
@JSONField(serialize = false)
public String getEnglishName2() {
System.out.println("getEnglishName2()执行!!");
return "lucy";
}
/**
* case2: getXxx()返回值为void
*/
public void getEnglishName3() {
System.out.println("getEnglishName3()执行!!");
}
/**
* case3: isXxx()返回值不等于布尔类型
*/
public String isChinaName2() {
System.out.println("isChinaName2()执行!!");
return "isChinaName2";
}
}
复制代码
実行結果は次のとおりです。
isChinaName()执行!!
getEnglishName()执行!!
{"chinaName":true,"englishName":"lucy"}
复制代码
コード仕様
可以看出来序列化的规则还是很多的,比如有时需要关注返回值,有时需要关注参数个数,有时需要关注@JSONType
注解,有时需要关注@JSONField
注解;当一个事物的判别方式有多种的时候,由于团队人员掌握知识点的程度不一样,这个方差很容易导致代码问题,所以尽量有一种推荐方案。 这里推荐使用@JSONField(serialize = false)
来显式的标注方法不参与序列化,下面是使用推荐方案后的代码,是不是一眼就能看出来哪些方法不需要参与序列化了。
public class CountryDTO {
private String country;
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return this.country;
}
@JSONField(serialize = false)
public static void queryCountryList() {
System.out.println("queryCountryList()执行!!");
}
public Boolean isChinaName() {
System.out.println("isChinaName()执行!!");
return true;
}
public String getEnglishName() {
System.out.println("getEnglishName()执行!!");
return "lucy";
}
@JSONField(serialize = false)
public String getOtherName() {
System.out.println("getOtherName()执行!!");
return "lucy";
}
@JSONField(serialize = false)
public String getEnglishName2() {
System.out.println("getEnglishName2()执行!!");
return "lucy";
}
@JSONField(serialize = false)
public void getEnglishName3() {
System.out.println("getEnglishName3()执行!!");
}
@JSONField(serialize = false)
public String isChinaName2() {
System.out.println("isChinaName2()执行!!");
return "isChinaName2";
}
}
复制代码
三个频率高的序列化的情况
以上流程基本遵循 发现问题 --> 原理分析 --> 解决问题 --> 升华(编程规范)。
- 围绕业务上:解决问题 -> 如何选择一种好的额解决方案 -> 好的解决方式如何扩展n个系统应用;
- 围绕技术上:解决单个问题,顺着单个问题掌握这条线上的原理。