修复字典数据报错

dev_xd
lj7788 2025-08-28 22:01:39 +08:00
parent 2f0f8f59b9
commit 6fa8b2e3d0
1 changed files with 39 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.yanzhu.api.domain.SysDictData; import com.yanzhu.api.domain.SysDictData;
import com.yanzhu.common.core.constant.CacheConstants; import com.yanzhu.common.core.constant.CacheConstants;
@ -37,10 +38,18 @@ public class DictUtils
*/ */
public static List<SysDictData> getDictCache(String key) public static List<SysDictData> getDictCache(String key)
{ {
JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); Object cacheObject = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
if (StringUtils.isNotNull(arrayCache)) 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; return null;
} }
@ -53,10 +62,20 @@ public class DictUtils
*/ */
public static SysDictData getDictCache(String key, String value) public static SysDictData getDictCache(String key, String value)
{ {
JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); Object cacheObject = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
if (StringUtils.isNotNull(arrayCache)) if (StringUtils.isNotNull(cacheObject))
{ {
List<SysDictData> dictList = arrayCache.toList(SysDictData.class); List<SysDictData> 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){ for(SysDictData dictData:dictList){
if(Objects.equals(value,dictData.getDictValue())){ if(Objects.equals(value,dictData.getDictValue())){
return dictData; return dictData;
@ -74,10 +93,20 @@ public class DictUtils
*/ */
public static String getDictLabel(String key, String value) public static String getDictLabel(String key, String value)
{ {
JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); Object cacheObject = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
if (StringUtils.isNotNull(arrayCache)) if (StringUtils.isNotNull(cacheObject))
{ {
List<SysDictData> dictList = arrayCache.toList(SysDictData.class); List<SysDictData> 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){ for(SysDictData dictData:dictList){
if(Objects.equals(value,dictData.getDictValue())){ if(Objects.equals(value,dictData.getDictValue())){
return dictData.getDictLabel(); return dictData.getDictLabel();
@ -116,4 +145,4 @@ public class DictUtils
{ {
return CacheConstants.SYS_DICT_KEY + configKey; return CacheConstants.SYS_DICT_KEY + configKey;
} }
} }