From 6fa8b2e3d02ebbc8777373d392b3b66f7db00d6e Mon Sep 17 00:00:00 2001 From: lj7788 Date: Thu, 28 Aug 2025 22:01:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=97=E5=85=B8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yanzhu/security/utils/DictUtils.java | 49 +++++++++++++++---- 1 file changed, 39 insertions(+), 10 deletions(-) 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