日常小妙招

代码

多个参数判断只用符合规则的

日常开发中一个入参对象里有多个参数,有几个属性需要判断是否为空或者是否符合某个条件,然后只取用符合条件的属性

1
2
3
private String getCanUseOne(String s1, String s2, String s3) {
return Stream.of(s1, s2, s3).filter(StrUtil::isBlank).findFirst().orElse(null);
}

使用fastjson反序列化带泛型的对象

开发中经常会遇到序列化和反序列化,带泛型的对象反序列化和一般的反序列化有点不同

结果封装对象

1
2
3
4
5
6
public class Result<T> {
private int code;
private String msg;
private T data;
}

业务对象

1
2
3
4
public class BusinessVo {
private String name;
private String value;
}

结果序列化

1
2
3
4
5
6
7
8
{
"code": 0,
"msg": "ok",
"data": {
"name": "xxx",
"value": "xxxxx"
}
}

使用TypeReference进行序列化

1
JSONObject.parseObject(json, new TypeReference<Result<BusinessVo>>(){});

软件

idea运行java代码提示命令过长

解决方案:

在项目.idea/workspace.xml中,找到标签<component name="PropertiesComponent">在里面添加一行<property name="dynamic.classpath" value="true" />