From fa3f382210d92197b57d4092e8eb78280f7dd18e Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 24 Jan 2021 12:37:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E4=B8=80=E6=AD=A5=E4=BC=98=E5=8C=96?= =?UTF-8?q?=20json=20=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=8C=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E4=BD=BF=E7=94=A8=20Spring=20=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E5=87=BA=E6=9D=A5=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jackson/config/JacksonConfig.java | 18 +++++++++++++ .../operatelog/core/aop/OperateLogAspect.java | 6 ++--- ...ndler.java => JsonLongSetTypeHandler.java} | 21 +++------------ .../AbstractChannelMessageListener.java | 4 +-- .../redis/core/util/RedisMessageUtils.java | 4 +-- .../dataobject/permission/SysRoleDO.java | 4 +-- .../dal/mysql/dataobject/user/SysUserDO.java | 4 +-- .../redis/dao/auth/SysLoginUserRedisDAO.java | 6 ++--- .../json/{JSONUtils.java => JsonUtils.java} | 26 +++++++++++++++---- .../dashboard/util/servlet/ServletUtils.java | 4 +-- 10 files changed, 59 insertions(+), 38 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java rename src/main/java/cn/iocoder/dashboard/framework/mybatis/core/type/{JacksonLongSetTypeHandler.java => JsonLongSetTypeHandler.java} (56%) rename src/main/java/cn/iocoder/dashboard/util/json/{JSONUtils.java => JsonUtils.java} (57%) diff --git a/src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java b/src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java new file mode 100644 index 000000000..27d199a2d --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java @@ -0,0 +1,18 @@ +package cn.iocoder.dashboard.framework.jackson.config; + +import cn.iocoder.dashboard.util.json.JsonUtils; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class JacksonConfig { + + @Bean + @SuppressWarnings("InstantiationOfUtilityClass") + public JsonUtils jsonUtils(ObjectMapper objectMapper) { + JsonUtils.init(objectMapper); + return new JsonUtils(); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/aop/OperateLogAspect.java b/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/aop/OperateLogAspect.java index 8f5dba063..0855b2b92 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/aop/OperateLogAspect.java +++ b/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/aop/OperateLogAspect.java @@ -11,7 +11,7 @@ import cn.iocoder.dashboard.framework.logger.operatelog.core.service.OperateLogF import cn.iocoder.dashboard.framework.security.core.util.SecurityUtils; import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogCreateReqVO; -import cn.iocoder.dashboard.util.json.JSONUtils; +import cn.iocoder.dashboard.util.json.JsonUtils; import cn.iocoder.dashboard.util.servlet.ServletUtils; import com.google.common.collect.Maps; import io.swagger.annotations.Api; @@ -319,7 +319,7 @@ public class OperateLogAspect { // 被忽略时,标记为 ignore 字符串,避免和 null 混在一起 args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]"); } - return JSONUtils.toJSONString(args); + return JsonUtils.toJsonString(args); } private static String obtainResultData(Object result) { @@ -327,7 +327,7 @@ public class OperateLogAspect { if (result instanceof CommonResult) { result = ((CommonResult) result).getData(); } - return JSONUtils.toJSONString(result); + return JsonUtils.toJsonString(result); } private static boolean isIgnoreArgs(Object object) { diff --git a/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/type/JacksonLongSetTypeHandler.java b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/type/JsonLongSetTypeHandler.java similarity index 56% rename from src/main/java/cn/iocoder/dashboard/framework/mybatis/core/type/JacksonLongSetTypeHandler.java rename to src/main/java/cn/iocoder/dashboard/framework/mybatis/core/type/JsonLongSetTypeHandler.java index d95ed3800..1f57353b6 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/type/JacksonLongSetTypeHandler.java +++ b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/type/JsonLongSetTypeHandler.java @@ -1,12 +1,10 @@ package cn.iocoder.dashboard.framework.mybatis.core.type; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; +import cn.iocoder.dashboard.util.json.JsonUtils; import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; import java.util.Set; /** @@ -17,29 +15,18 @@ import java.util.Set; * * @author 芋道源码 */ -public class JacksonLongSetTypeHandler extends AbstractJsonTypeHandler { - - // TODO 芋艿,需要将 Spring 的设置下进来 - private static final ObjectMapper objectMapper = new ObjectMapper(); +public class JsonLongSetTypeHandler extends AbstractJsonTypeHandler { private static final TypeReference> typeReference = new TypeReference>(){}; @Override protected Object parse(String json) { - try { - return objectMapper.readValue(json, typeReference); - } catch (IOException e) { - throw new RuntimeException(e); - } + return JsonUtils.parseObject(json, typeReference); } @Override protected String toJson(Object obj) { - try { - return objectMapper.writeValueAsString(obj); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } + return JsonUtils.toJsonString(obj); } } diff --git a/src/main/java/cn/iocoder/dashboard/framework/redis/core/pubsub/AbstractChannelMessageListener.java b/src/main/java/cn/iocoder/dashboard/framework/redis/core/pubsub/AbstractChannelMessageListener.java index 98443e491..23b40e228 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/redis/core/pubsub/AbstractChannelMessageListener.java +++ b/src/main/java/cn/iocoder/dashboard/framework/redis/core/pubsub/AbstractChannelMessageListener.java @@ -1,7 +1,7 @@ package cn.iocoder.dashboard.framework.redis.core.pubsub; import cn.hutool.core.util.ArrayUtil; -import cn.iocoder.dashboard.util.json.JSONUtils; +import cn.iocoder.dashboard.util.json.JsonUtils; import lombok.SneakyThrows; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; @@ -44,7 +44,7 @@ public abstract class AbstractChannelMessageListener i @Override public final void onMessage(Message message, byte[] bytes) { - T messageObj = JSONUtils.parseObject(message.getBody(), messageType); + T messageObj = JsonUtils.parseObject(message.getBody(), messageType); this.onMessage(messageObj); } diff --git a/src/main/java/cn/iocoder/dashboard/framework/redis/core/util/RedisMessageUtils.java b/src/main/java/cn/iocoder/dashboard/framework/redis/core/util/RedisMessageUtils.java index 2db401629..b5cd3780b 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/redis/core/util/RedisMessageUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/redis/core/util/RedisMessageUtils.java @@ -1,7 +1,7 @@ package cn.iocoder.dashboard.framework.redis.core.util; import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage; -import cn.iocoder.dashboard.util.json.JSONUtils; +import cn.iocoder.dashboard.util.json.JsonUtils; import org.springframework.data.redis.core.RedisTemplate; /** @@ -18,7 +18,7 @@ public class RedisMessageUtils { * @param message 消息 */ public static void sendChannelMessage(RedisTemplate redisTemplate, T message) { - redisTemplate.convertAndSend(message.getChannel(), JSONUtils.toJSONString(message)); + redisTemplate.convertAndSend(message.getChannel(), JsonUtils.toJsonString(message)); } } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDO.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDO.java index 18bc788cf..5b910a65a 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDO.java @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; -import cn.iocoder.dashboard.framework.mybatis.core.type.JacksonLongSetTypeHandler; +import cn.iocoder.dashboard.framework.mybatis.core.type.JsonLongSetTypeHandler; import cn.iocoder.dashboard.framework.security.core.enums.DataScopeEnum; import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum; import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum; @@ -71,7 +71,7 @@ public class SysRoleDO extends BaseDO { * * 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM} 时 */ - @TableField(typeHandler = JacksonLongSetTypeHandler.class) + @TableField(typeHandler = JsonLongSetTypeHandler.class) private Set dataScopeDeptIds; } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/user/SysUserDO.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/user/SysUserDO.java index 29a056010..0bff23b91 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/user/SysUserDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/user/SysUserDO.java @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; -import cn.iocoder.dashboard.framework.mybatis.core.type.JacksonLongSetTypeHandler; +import cn.iocoder.dashboard.framework.mybatis.core.type.JsonLongSetTypeHandler; import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; @@ -54,7 +54,7 @@ public class SysUserDO extends BaseDO { /** * 岗位编号数组 */ - @TableField(typeHandler = JacksonLongSetTypeHandler.class) + @TableField(typeHandler = JsonLongSetTypeHandler.class) private Set postIds; /** * 用户邮箱 diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/dao/auth/SysLoginUserRedisDAO.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/dao/auth/SysLoginUserRedisDAO.java index 0bb954c43..0b54d4535 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/dao/auth/SysLoginUserRedisDAO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/dao/auth/SysLoginUserRedisDAO.java @@ -1,7 +1,7 @@ package cn.iocoder.dashboard.modules.system.dal.redis.dao.auth; import cn.iocoder.dashboard.framework.security.core.LoginUser; -import cn.iocoder.dashboard.util.json.JSONUtils; +import cn.iocoder.dashboard.util.json.JsonUtils; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Repository; @@ -22,12 +22,12 @@ public class SysLoginUserRedisDAO { public LoginUser get(String sessionId) { String redisKey = formatKey(sessionId); - return JSONUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), LoginUser.class); + return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), LoginUser.class); } public void set(String sessionId, LoginUser loginUser) { String redisKey = formatKey(sessionId); - stringRedisTemplate.opsForValue().set(redisKey, JSONUtils.toJSONString(loginUser), LOGIN_USER.getTimeout()); + stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(loginUser), LOGIN_USER.getTimeout()); } public void delete(String accessToken) { diff --git a/src/main/java/cn/iocoder/dashboard/util/json/JSONUtils.java b/src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java similarity index 57% rename from src/main/java/cn/iocoder/dashboard/util/json/JSONUtils.java rename to src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java index 8a7521758..d1ef1cda3 100644 --- a/src/main/java/cn/iocoder/dashboard/util/json/JSONUtils.java +++ b/src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java @@ -1,25 +1,33 @@ package cn.iocoder.dashboard.util.json; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; +import java.util.Set; /** * JSON 工具类 * * @author 芋道源码 */ -public class JSONUtils { +public class JsonUtils { private static ObjectMapper objectMapper = new ObjectMapper(); - // TODO 芋艿,需要将 Spring 的设置下进来 - public static void setObjectMapper(ObjectMapper objectMapper) { - JSONUtils.objectMapper = objectMapper; + /** + * 初始化 objectMapper 属性 + * + * 通过这样的方式,使用 Spring 创建的 ObjectMapper Bean + * + * @param objectMapper ObjectMapper 对象 + */ + public static void init(ObjectMapper objectMapper) { + JsonUtils.objectMapper = objectMapper; } - public static String toJSONString(Object object) { + public static String toJsonString(Object object) { try { return objectMapper.writeValueAsString(object); } catch (JsonProcessingException e) { @@ -43,4 +51,12 @@ public class JSONUtils { } } + public static Object parseObject(String text, TypeReference> typeReference) { + try { + return objectMapper.readValue(text, typeReference); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } diff --git a/src/main/java/cn/iocoder/dashboard/util/servlet/ServletUtils.java b/src/main/java/cn/iocoder/dashboard/util/servlet/ServletUtils.java index e3cb86d77..35f3d5765 100644 --- a/src/main/java/cn/iocoder/dashboard/util/servlet/ServletUtils.java +++ b/src/main/java/cn/iocoder/dashboard/util/servlet/ServletUtils.java @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.util.servlet; import cn.hutool.core.io.IoUtil; import cn.hutool.extra.servlet.ServletUtil; -import cn.iocoder.dashboard.util.json.JSONUtils; +import cn.iocoder.dashboard.util.json.JsonUtils; import org.springframework.http.MediaType; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; @@ -28,7 +28,7 @@ public class ServletUtils { */ @SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE,否则会乱码 public static void writeJSON(HttpServletResponse response, Object object) { - String content = JSONUtils.toJSONString(object); + String content = JsonUtils.toJsonString(object); ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE); }