mirror of https://gitee.com/stylefeng/roses
【7.2.5】【wrapper】字段格式化增加异常处理、增加对用户id的字段格式化
parent
53e5512eb5
commit
3cbea40d4a
|
@ -0,0 +1,53 @@
|
|||
package cn.stylefeng.roses.kernel.wrapper.api.util;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.stylefeng.roses.kernel.rule.util.ObjectConvertUtil;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 字段类型判断
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2022/9/7 10:24
|
||||
*/
|
||||
public class MixFieldTypeUtil {
|
||||
|
||||
/**
|
||||
* 判断fieldValue是否是Long、List<Long>、Long[]类型,如果是其中任何一种,则都返回true
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2022/9/7 10:24
|
||||
*/
|
||||
public static boolean getLongFlag(Object fieldValue) {
|
||||
|
||||
// 直接判断是否是Long
|
||||
if (fieldValue instanceof Long) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 如果是集合类型
|
||||
else if (fieldValue instanceof Collection) {
|
||||
Collection<?> collectionList = (Collection<?>) fieldValue;
|
||||
for (Object item : collectionList) {
|
||||
if (item instanceof Long) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是数组类型
|
||||
else if (ArrayUtil.isArray(fieldValue)) {
|
||||
Object[] objects = ObjectConvertUtil.objToArray(fieldValue);
|
||||
for (Object item : objects) {
|
||||
if (item instanceof Long) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -8,6 +8,8 @@ import com.fasterxml.jackson.databind.JsonSerializer;
|
|||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 针对@EnumFieldFormat注解的具体序列化过程
|
||||
*
|
||||
|
@ -33,7 +35,7 @@ public class EnumFieldFormatSerializer extends JsonSerializer<Object> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
|
||||
public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||
|
||||
// 最终转化的值
|
||||
Object formattedValue = originValue;
|
||||
|
|
|
@ -8,6 +8,8 @@ import com.fasterxml.jackson.databind.JsonSerializer;
|
|||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 针对@SimpleFieldFormat注解的具体序列化过程
|
||||
*
|
||||
|
@ -33,20 +35,31 @@ public class SimpleFieldFormatSerializer extends JsonSerializer<Object> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
|
||||
public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||
try {
|
||||
this.action(originValue, jsonGenerator, serializerProvider);
|
||||
} catch (Exception e) {
|
||||
log.error("执行json的字段序列化出错", e);
|
||||
// 报错后继续写入原始值,否则会响应的json不是规范的json
|
||||
jsonGenerator.writeObject(originValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 真正处理序列化的逻辑
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2022/9/7 11:11
|
||||
*/
|
||||
private void action(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws InstantiationException, IllegalAccessException, IOException {
|
||||
|
||||
// 创建具体字段转化的实现类
|
||||
SimpleFieldFormatProcess simpleFieldFormatProcess = null;
|
||||
try {
|
||||
simpleFieldFormatProcess = processClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
log.error("执行json的字段序列化出错", e);
|
||||
return;
|
||||
}
|
||||
SimpleFieldFormatProcess simpleFieldFormatProcess = processClass.newInstance();
|
||||
|
||||
// 判断当前字段值是否可以转化
|
||||
boolean canFormat = simpleFieldFormatProcess.canFormat(originValue);
|
||||
if (!canFormat) {
|
||||
jsonGenerator.writeObject(originValue);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,44 +28,39 @@ public class CommonFormatUtil {
|
|||
* @author fengshuonan
|
||||
* @date 2022/9/6 17:16
|
||||
*/
|
||||
public static void writeField(FormatTypeEnum formatTypeEnum, Object originValue, Object formattedValue, JsonGenerator jsonGenerator) {
|
||||
try {
|
||||
// 如果原始值和转化值一样,则直接返回
|
||||
if (originValue.equals(formattedValue)) {
|
||||
jsonGenerator.writeObject(originValue);
|
||||
return;
|
||||
public static void writeField(FormatTypeEnum formatTypeEnum, Object originValue, Object formattedValue, JsonGenerator jsonGenerator) throws IOException {
|
||||
// 如果原始值和转化值一样,则直接返回
|
||||
if (originValue.equals(formattedValue)) {
|
||||
jsonGenerator.writeObject(originValue);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果转化模式是替换类型
|
||||
if (formatTypeEnum.equals(FormatTypeEnum.REPLACE)) {
|
||||
jsonGenerator.writeObject(formattedValue);
|
||||
}
|
||||
|
||||
// 如果转化模式是新增一个包装字段
|
||||
else {
|
||||
// 先写入原有值,保持不变
|
||||
jsonGenerator.writeObject(originValue);
|
||||
|
||||
// 构造新的字段名,为原字段名+Wrapper
|
||||
String fieldName = jsonGenerator.getOutputContext().getCurrentName();
|
||||
String newWrapperFieldName = fieldName + WrapperConstants.FILED_WRAPPER_SUFFIX;
|
||||
|
||||
// 获取当前正在转化的对象
|
||||
Object currentObj = jsonGenerator.getOutputContext().getCurrentValue();
|
||||
|
||||
// 如果当前正在转化的对象中已经含有了字段名+Wrapper的字段,则生成时候带一个数字2
|
||||
Field declaredField = ClassUtil.getDeclaredField(currentObj.getClass(), newWrapperFieldName);
|
||||
if (declaredField != null) {
|
||||
newWrapperFieldName = newWrapperFieldName + "2";
|
||||
}
|
||||
|
||||
// 如果转化模式是替换类型
|
||||
if (formatTypeEnum.equals(FormatTypeEnum.REPLACE)) {
|
||||
jsonGenerator.writeObject(formattedValue);
|
||||
}
|
||||
|
||||
// 如果转化模式是新增一个包装字段
|
||||
else {
|
||||
// 先写入原有值,保持不变
|
||||
jsonGenerator.writeObject(originValue);
|
||||
|
||||
// 构造新的字段名,为原字段名+Wrapper
|
||||
String fieldName = jsonGenerator.getOutputContext().getCurrentName();
|
||||
String newWrapperFieldName = fieldName + WrapperConstants.FILED_WRAPPER_SUFFIX;
|
||||
|
||||
// 获取当前正在转化的对象
|
||||
Object currentObj = jsonGenerator.getOutputContext().getCurrentValue();
|
||||
|
||||
// 如果当前正在转化的对象中已经含有了字段名+Wrapper的字段,则生成时候带一个数字2
|
||||
Field declaredField = ClassUtil.getDeclaredField(currentObj.getClass(), newWrapperFieldName);
|
||||
if (declaredField != null) {
|
||||
newWrapperFieldName = newWrapperFieldName + "2";
|
||||
}
|
||||
|
||||
// 写入新的字段名
|
||||
jsonGenerator.writeFieldName(newWrapperFieldName);
|
||||
jsonGenerator.writeObject(formattedValue);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
log.error("执行json的字段序列化出错", e);
|
||||
// 写入新的字段名
|
||||
jsonGenerator.writeFieldName(newWrapperFieldName);
|
||||
jsonGenerator.writeObject(formattedValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
package cn.stylefeng.roses.kernel.system.modular.user.format;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.stylefeng.roses.kernel.rule.base.SimpleFieldFormatProcess;
|
||||
import cn.stylefeng.roses.kernel.rule.util.ObjectConvertUtil;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.enums.FieldTypeEnum;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.util.ClassTypeUtil;
|
||||
import cn.stylefeng.roses.kernel.system.api.UserServiceApi;
|
||||
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO;
|
||||
import cn.stylefeng.roses.kernel.wrapper.api.util.MixFieldTypeUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Json响应的针对用户的处理
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2022/9/7 10:09
|
||||
*/
|
||||
public class UserFormatProcess implements SimpleFieldFormatProcess {
|
||||
|
||||
@Override
|
||||
public boolean canFormat(Object originValue) {
|
||||
return MixFieldTypeUtil.getLongFlag(originValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object formatProcess(Object originValue) {
|
||||
|
||||
// 先获取是基础类型还是集合类型
|
||||
FieldTypeEnum classFieldType = ClassTypeUtil.getClassFieldType(originValue.getClass());
|
||||
|
||||
// 基础类型,直接转化
|
||||
if (FieldTypeEnum.BASIC.equals(classFieldType)) {
|
||||
Long userId = Convert.toLong(originValue);
|
||||
return this.userIdToName(userId);
|
||||
}
|
||||
|
||||
// 集合类型
|
||||
if (FieldTypeEnum.BASE_COLLECTION.equals(classFieldType)) {
|
||||
Collection<?> userIdList = (Collection<?>) originValue;
|
||||
List<String> nameList = new ArrayList<>();
|
||||
for (Object userIdStr : userIdList) {
|
||||
Long userId = Convert.toLong(userIdStr);
|
||||
nameList.add(this.userIdToName(userId));
|
||||
}
|
||||
return nameList;
|
||||
}
|
||||
|
||||
// 数组类型
|
||||
if (FieldTypeEnum.BASE_ARRAY.equals(classFieldType)) {
|
||||
Object[] objects = ObjectConvertUtil.objToArray(originValue);
|
||||
List<String> nameList = new ArrayList<>();
|
||||
for (Object userIdStr : objects) {
|
||||
Long userId = Convert.toLong(userIdStr);
|
||||
nameList.add(this.userIdToName(userId));
|
||||
}
|
||||
return nameList;
|
||||
}
|
||||
|
||||
return originValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户id转化为姓名的过程
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2022/9/7 10:59
|
||||
*/
|
||||
private String userIdToName(Long userId) {
|
||||
UserServiceApi bean = SpringUtil.getBean(UserServiceApi.class);
|
||||
SysUserDTO userInfoByUserId = bean.getUserInfoByUserId(userId);
|
||||
return userInfoByUserId.getRealName();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue