diff --git a/yanzhu-common/yanzhu-common-mapper/src/main/java/com/yanzhu/security/utils/DictUtils.java b/yanzhu-common/yanzhu-common-mapper/src/main/java/com/yanzhu/security/utils/DictUtils.java index a41e838b..51710824 100644 --- a/yanzhu-common/yanzhu-common-mapper/src/main/java/com/yanzhu/security/utils/DictUtils.java +++ b/yanzhu-common/yanzhu-common-mapper/src/main/java/com/yanzhu/security/utils/DictUtils.java @@ -4,6 +4,7 @@ import java.util.Collection; import java.util.List; import java.util.Objects; +import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.yanzhu.api.domain.SysDictData; import com.yanzhu.common.core.constant.CacheConstants; @@ -37,10 +38,18 @@ public class DictUtils */ public static List getDictCache(String key) { - JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); - if (StringUtils.isNotNull(arrayCache)) + Object cacheObject = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); + if (StringUtils.isNotNull(cacheObject)) { - return arrayCache.toList(SysDictData.class); + // 使用 FastJSON2 进行反序列化,避免 Jackson 序列化问题 + if (cacheObject instanceof JSONArray) { + return ((JSONArray) cacheObject).toList(SysDictData.class); + } else if (cacheObject instanceof String) { + return JSON.parseArray((String) cacheObject, SysDictData.class); + } else { + // 如果是其他类型,尝试直接转换 + return JSON.parseArray(JSON.toJSONString(cacheObject), SysDictData.class); + } } return null; } @@ -53,10 +62,20 @@ public class DictUtils */ public static SysDictData getDictCache(String key, String value) { - JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); - if (StringUtils.isNotNull(arrayCache)) + Object cacheObject = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); + if (StringUtils.isNotNull(cacheObject)) { - List dictList = arrayCache.toList(SysDictData.class); + List dictList; + // 使用 FastJSON2 进行反序列化,避免 Jackson 序列化问题 + if (cacheObject instanceof JSONArray) { + dictList = ((JSONArray) cacheObject).toList(SysDictData.class); + } else if (cacheObject instanceof String) { + dictList = JSON.parseArray((String) cacheObject, SysDictData.class); + } else { + // 如果是其他类型,尝试直接转换 + dictList = JSON.parseArray(JSON.toJSONString(cacheObject), SysDictData.class); + } + for(SysDictData dictData:dictList){ if(Objects.equals(value,dictData.getDictValue())){ return dictData; @@ -74,10 +93,20 @@ public class DictUtils */ public static String getDictLabel(String key, String value) { - JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); - if (StringUtils.isNotNull(arrayCache)) + Object cacheObject = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); + if (StringUtils.isNotNull(cacheObject)) { - List dictList = arrayCache.toList(SysDictData.class); + List dictList; + // 使用 FastJSON2 进行反序列化,避免 Jackson 序列化问题 + if (cacheObject instanceof JSONArray) { + dictList = ((JSONArray) cacheObject).toList(SysDictData.class); + } else if (cacheObject instanceof String) { + dictList = JSON.parseArray((String) cacheObject, SysDictData.class); + } else { + // 如果是其他类型,尝试直接转换 + dictList = JSON.parseArray(JSON.toJSONString(cacheObject), SysDictData.class); + } + for(SysDictData dictData:dictList){ if(Objects.equals(value,dictData.getDictValue())){ return dictData.getDictLabel(); @@ -116,4 +145,4 @@ public class DictUtils { return CacheConstants.SYS_DICT_KEY + configKey; } -} +} \ No newline at end of file