Commit fa3f382210d92197b57d4092e8eb78280f7dd18e
1 parent
eadc4f74
进一步优化 json 工具类,默认使用 Spring 初始化出来的
Showing
10 changed files
with
59 additions
and
38 deletions
src/main/java/cn/iocoder/dashboard/framework/jackson/config/JacksonConfig.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.framework.jackson.config; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.dashboard.util.json.JsonUtils; | ||
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 5 | +import org.springframework.context.annotation.Bean; | ||
| 6 | +import org.springframework.context.annotation.Configuration; | ||
| 7 | + | ||
| 8 | +@Configuration | ||
| 9 | +public class JacksonConfig { | ||
| 10 | + | ||
| 11 | + @Bean | ||
| 12 | + @SuppressWarnings("InstantiationOfUtilityClass") | ||
| 13 | + public JsonUtils jsonUtils(ObjectMapper objectMapper) { | ||
| 14 | + JsonUtils.init(objectMapper); | ||
| 15 | + return new JsonUtils(); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | +} |
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 | @@ -11,7 +11,7 @@ import cn.iocoder.dashboard.framework.logger.operatelog.core.service.OperateLogF | ||
| 11 | import cn.iocoder.dashboard.framework.security.core.util.SecurityUtils; | 11 | import cn.iocoder.dashboard.framework.security.core.util.SecurityUtils; |
| 12 | import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; | 12 | import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; |
| 13 | import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogCreateReqVO; | 13 | import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogCreateReqVO; |
| 14 | -import cn.iocoder.dashboard.util.json.JSONUtils; | 14 | +import cn.iocoder.dashboard.util.json.JsonUtils; |
| 15 | import cn.iocoder.dashboard.util.servlet.ServletUtils; | 15 | import cn.iocoder.dashboard.util.servlet.ServletUtils; |
| 16 | import com.google.common.collect.Maps; | 16 | import com.google.common.collect.Maps; |
| 17 | import io.swagger.annotations.Api; | 17 | import io.swagger.annotations.Api; |
| @@ -319,7 +319,7 @@ public class OperateLogAspect { | @@ -319,7 +319,7 @@ public class OperateLogAspect { | ||
| 319 | // 被忽略时,标记为 ignore 字符串,避免和 null 混在一起 | 319 | // 被忽略时,标记为 ignore 字符串,避免和 null 混在一起 |
| 320 | args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]"); | 320 | args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]"); |
| 321 | } | 321 | } |
| 322 | - return JSONUtils.toJSONString(args); | 322 | + return JsonUtils.toJsonString(args); |
| 323 | } | 323 | } |
| 324 | 324 | ||
| 325 | private static String obtainResultData(Object result) { | 325 | private static String obtainResultData(Object result) { |
| @@ -327,7 +327,7 @@ public class OperateLogAspect { | @@ -327,7 +327,7 @@ public class OperateLogAspect { | ||
| 327 | if (result instanceof CommonResult) { | 327 | if (result instanceof CommonResult) { |
| 328 | result = ((CommonResult<?>) result).getData(); | 328 | result = ((CommonResult<?>) result).getData(); |
| 329 | } | 329 | } |
| 330 | - return JSONUtils.toJSONString(result); | 330 | + return JsonUtils.toJsonString(result); |
| 331 | } | 331 | } |
| 332 | 332 | ||
| 333 | private static boolean isIgnoreArgs(Object object) { | 333 | private static boolean isIgnoreArgs(Object object) { |
src/main/java/cn/iocoder/dashboard/framework/mybatis/core/type/JacksonLongSetTypeHandler.java renamed to src/main/java/cn/iocoder/dashboard/framework/mybatis/core/type/JsonLongSetTypeHandler.java
| 1 | package cn.iocoder.dashboard.framework.mybatis.core.type; | 1 | package cn.iocoder.dashboard.framework.mybatis.core.type; |
| 2 | 2 | ||
| 3 | import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; | 3 | import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; |
| 4 | +import cn.iocoder.dashboard.util.json.JsonUtils; | ||
| 4 | import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler; | 5 | import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler; |
| 5 | -import com.fasterxml.jackson.core.JsonProcessingException; | ||
| 6 | import com.fasterxml.jackson.core.type.TypeReference; | 6 | import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 8 | 7 | ||
| 9 | -import java.io.IOException; | ||
| 10 | import java.util.Set; | 8 | import java.util.Set; |
| 11 | 9 | ||
| 12 | /** | 10 | /** |
| @@ -17,29 +15,18 @@ import java.util.Set; | @@ -17,29 +15,18 @@ import java.util.Set; | ||
| 17 | * | 15 | * |
| 18 | * @author 芋道源码 | 16 | * @author 芋道源码 |
| 19 | */ | 17 | */ |
| 20 | -public class JacksonLongSetTypeHandler extends AbstractJsonTypeHandler<Object> { | ||
| 21 | - | ||
| 22 | - // TODO 芋艿,需要将 Spring 的设置下进来 | ||
| 23 | - private static final ObjectMapper objectMapper = new ObjectMapper(); | 18 | +public class JsonLongSetTypeHandler extends AbstractJsonTypeHandler<Object> { |
| 24 | 19 | ||
| 25 | private static final TypeReference<Set<Long>> typeReference = new TypeReference<Set<Long>>(){}; | 20 | private static final TypeReference<Set<Long>> typeReference = new TypeReference<Set<Long>>(){}; |
| 26 | 21 | ||
| 27 | @Override | 22 | @Override |
| 28 | protected Object parse(String json) { | 23 | protected Object parse(String json) { |
| 29 | - try { | ||
| 30 | - return objectMapper.readValue(json, typeReference); | ||
| 31 | - } catch (IOException e) { | ||
| 32 | - throw new RuntimeException(e); | ||
| 33 | - } | 24 | + return JsonUtils.parseObject(json, typeReference); |
| 34 | } | 25 | } |
| 35 | 26 | ||
| 36 | @Override | 27 | @Override |
| 37 | protected String toJson(Object obj) { | 28 | protected String toJson(Object obj) { |
| 38 | - try { | ||
| 39 | - return objectMapper.writeValueAsString(obj); | ||
| 40 | - } catch (JsonProcessingException e) { | ||
| 41 | - throw new RuntimeException(e); | ||
| 42 | - } | 29 | + return JsonUtils.toJsonString(obj); |
| 43 | } | 30 | } |
| 44 | 31 | ||
| 45 | } | 32 | } |
src/main/java/cn/iocoder/dashboard/framework/redis/core/pubsub/AbstractChannelMessageListener.java
| 1 | package cn.iocoder.dashboard.framework.redis.core.pubsub; | 1 | package cn.iocoder.dashboard.framework.redis.core.pubsub; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.ArrayUtil; | 3 | import cn.hutool.core.util.ArrayUtil; |
| 4 | -import cn.iocoder.dashboard.util.json.JSONUtils; | 4 | +import cn.iocoder.dashboard.util.json.JsonUtils; |
| 5 | import lombok.SneakyThrows; | 5 | import lombok.SneakyThrows; |
| 6 | import org.springframework.data.redis.connection.Message; | 6 | import org.springframework.data.redis.connection.Message; |
| 7 | import org.springframework.data.redis.connection.MessageListener; | 7 | import org.springframework.data.redis.connection.MessageListener; |
| @@ -44,7 +44,7 @@ public abstract class AbstractChannelMessageListener<T extends ChannelMessage> i | @@ -44,7 +44,7 @@ public abstract class AbstractChannelMessageListener<T extends ChannelMessage> i | ||
| 44 | 44 | ||
| 45 | @Override | 45 | @Override |
| 46 | public final void onMessage(Message message, byte[] bytes) { | 46 | public final void onMessage(Message message, byte[] bytes) { |
| 47 | - T messageObj = JSONUtils.parseObject(message.getBody(), messageType); | 47 | + T messageObj = JsonUtils.parseObject(message.getBody(), messageType); |
| 48 | this.onMessage(messageObj); | 48 | this.onMessage(messageObj); |
| 49 | } | 49 | } |
| 50 | 50 |
src/main/java/cn/iocoder/dashboard/framework/redis/core/util/RedisMessageUtils.java
| 1 | package cn.iocoder.dashboard.framework.redis.core.util; | 1 | package cn.iocoder.dashboard.framework.redis.core.util; |
| 2 | 2 | ||
| 3 | import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage; | 3 | import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage; |
| 4 | -import cn.iocoder.dashboard.util.json.JSONUtils; | 4 | +import cn.iocoder.dashboard.util.json.JsonUtils; |
| 5 | import org.springframework.data.redis.core.RedisTemplate; | 5 | import org.springframework.data.redis.core.RedisTemplate; |
| 6 | 6 | ||
| 7 | /** | 7 | /** |
| @@ -18,7 +18,7 @@ public class RedisMessageUtils { | @@ -18,7 +18,7 @@ public class RedisMessageUtils { | ||
| 18 | * @param message 消息 | 18 | * @param message 消息 |
| 19 | */ | 19 | */ |
| 20 | public static <T extends ChannelMessage> void sendChannelMessage(RedisTemplate<?, ?> redisTemplate, T message) { | 20 | public static <T extends ChannelMessage> void sendChannelMessage(RedisTemplate<?, ?> redisTemplate, T message) { |
| 21 | - redisTemplate.convertAndSend(message.getChannel(), JSONUtils.toJSONString(message)); | 21 | + redisTemplate.convertAndSend(message.getChannel(), JsonUtils.toJsonString(message)); |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | } | 24 | } |
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; | @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; | 3 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| 4 | import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; | 4 | import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; |
| 5 | -import cn.iocoder.dashboard.framework.mybatis.core.type.JacksonLongSetTypeHandler; | 5 | +import cn.iocoder.dashboard.framework.mybatis.core.type.JsonLongSetTypeHandler; |
| 6 | import cn.iocoder.dashboard.framework.security.core.enums.DataScopeEnum; | 6 | import cn.iocoder.dashboard.framework.security.core.enums.DataScopeEnum; |
| 7 | import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum; | 7 | import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum; |
| 8 | import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum; | 8 | import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum; |
| @@ -71,7 +71,7 @@ public class SysRoleDO extends BaseDO { | @@ -71,7 +71,7 @@ public class SysRoleDO extends BaseDO { | ||
| 71 | * | 71 | * |
| 72 | * 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM} 时 | 72 | * 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM} 时 |
| 73 | */ | 73 | */ |
| 74 | - @TableField(typeHandler = JacksonLongSetTypeHandler.class) | 74 | + @TableField(typeHandler = JsonLongSetTypeHandler.class) |
| 75 | private Set<Long> dataScopeDeptIds; | 75 | private Set<Long> dataScopeDeptIds; |
| 76 | 76 | ||
| 77 | } | 77 | } |
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; | @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; | 3 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| 4 | import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; | 4 | import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; |
| 5 | -import cn.iocoder.dashboard.framework.mybatis.core.type.JacksonLongSetTypeHandler; | 5 | +import cn.iocoder.dashboard.framework.mybatis.core.type.JsonLongSetTypeHandler; |
| 6 | import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum; | 6 | import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum; |
| 7 | import com.baomidou.mybatisplus.annotation.TableField; | 7 | import com.baomidou.mybatisplus.annotation.TableField; |
| 8 | import com.baomidou.mybatisplus.annotation.TableId; | 8 | import com.baomidou.mybatisplus.annotation.TableId; |
| @@ -54,7 +54,7 @@ public class SysUserDO extends BaseDO { | @@ -54,7 +54,7 @@ public class SysUserDO extends BaseDO { | ||
| 54 | /** | 54 | /** |
| 55 | * 岗位编号数组 | 55 | * 岗位编号数组 |
| 56 | */ | 56 | */ |
| 57 | - @TableField(typeHandler = JacksonLongSetTypeHandler.class) | 57 | + @TableField(typeHandler = JsonLongSetTypeHandler.class) |
| 58 | private Set<Long> postIds; | 58 | private Set<Long> postIds; |
| 59 | /** | 59 | /** |
| 60 | * 用户邮箱 | 60 | * 用户邮箱 |
src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/dao/auth/SysLoginUserRedisDAO.java
| 1 | package cn.iocoder.dashboard.modules.system.dal.redis.dao.auth; | 1 | package cn.iocoder.dashboard.modules.system.dal.redis.dao.auth; |
| 2 | 2 | ||
| 3 | import cn.iocoder.dashboard.framework.security.core.LoginUser; | 3 | import cn.iocoder.dashboard.framework.security.core.LoginUser; |
| 4 | -import cn.iocoder.dashboard.util.json.JSONUtils; | 4 | +import cn.iocoder.dashboard.util.json.JsonUtils; |
| 5 | import org.springframework.data.redis.core.StringRedisTemplate; | 5 | import org.springframework.data.redis.core.StringRedisTemplate; |
| 6 | import org.springframework.stereotype.Repository; | 6 | import org.springframework.stereotype.Repository; |
| 7 | 7 | ||
| @@ -22,12 +22,12 @@ public class SysLoginUserRedisDAO { | @@ -22,12 +22,12 @@ public class SysLoginUserRedisDAO { | ||
| 22 | 22 | ||
| 23 | public LoginUser get(String sessionId) { | 23 | public LoginUser get(String sessionId) { |
| 24 | String redisKey = formatKey(sessionId); | 24 | String redisKey = formatKey(sessionId); |
| 25 | - return JSONUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), LoginUser.class); | 25 | + return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), LoginUser.class); |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | public void set(String sessionId, LoginUser loginUser) { | 28 | public void set(String sessionId, LoginUser loginUser) { |
| 29 | String redisKey = formatKey(sessionId); | 29 | String redisKey = formatKey(sessionId); |
| 30 | - stringRedisTemplate.opsForValue().set(redisKey, JSONUtils.toJSONString(loginUser), LOGIN_USER.getTimeout()); | 30 | + stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(loginUser), LOGIN_USER.getTimeout()); |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | public void delete(String accessToken) { | 33 | public void delete(String accessToken) { |
src/main/java/cn/iocoder/dashboard/util/json/JSONUtils.java renamed to src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java
| 1 | package cn.iocoder.dashboard.util.json; | 1 | package cn.iocoder.dashboard.util.json; |
| 2 | 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | 3 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; | ||
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | 6 | ||
| 6 | import java.io.IOException; | 7 | import java.io.IOException; |
| 8 | +import java.util.Set; | ||
| 7 | 9 | ||
| 8 | /** | 10 | /** |
| 9 | * JSON 工具类 | 11 | * JSON 工具类 |
| 10 | * | 12 | * |
| 11 | * @author 芋道源码 | 13 | * @author 芋道源码 |
| 12 | */ | 14 | */ |
| 13 | -public class JSONUtils { | 15 | +public class JsonUtils { |
| 14 | 16 | ||
| 15 | private static ObjectMapper objectMapper = new ObjectMapper(); | 17 | private static ObjectMapper objectMapper = new ObjectMapper(); |
| 16 | 18 | ||
| 17 | - // TODO 芋艿,需要将 Spring 的设置下进来 | ||
| 18 | - public static void setObjectMapper(ObjectMapper objectMapper) { | ||
| 19 | - JSONUtils.objectMapper = objectMapper; | 19 | + /** |
| 20 | + * 初始化 objectMapper 属性 | ||
| 21 | + * | ||
| 22 | + * 通过这样的方式,使用 Spring 创建的 ObjectMapper Bean | ||
| 23 | + * | ||
| 24 | + * @param objectMapper ObjectMapper 对象 | ||
| 25 | + */ | ||
| 26 | + public static void init(ObjectMapper objectMapper) { | ||
| 27 | + JsonUtils.objectMapper = objectMapper; | ||
| 20 | } | 28 | } |
| 21 | 29 | ||
| 22 | - public static String toJSONString(Object object) { | 30 | + public static String toJsonString(Object object) { |
| 23 | try { | 31 | try { |
| 24 | return objectMapper.writeValueAsString(object); | 32 | return objectMapper.writeValueAsString(object); |
| 25 | } catch (JsonProcessingException e) { | 33 | } catch (JsonProcessingException e) { |
| @@ -43,4 +51,12 @@ public class JSONUtils { | @@ -43,4 +51,12 @@ public class JSONUtils { | ||
| 43 | } | 51 | } |
| 44 | } | 52 | } |
| 45 | 53 | ||
| 54 | + public static Object parseObject(String text, TypeReference<Set<Long>> typeReference) { | ||
| 55 | + try { | ||
| 56 | + return objectMapper.readValue(text, typeReference); | ||
| 57 | + } catch (IOException e) { | ||
| 58 | + throw new RuntimeException(e); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 46 | } | 62 | } |
src/main/java/cn/iocoder/dashboard/util/servlet/ServletUtils.java
| @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.util.servlet; | @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.util.servlet; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.io.IoUtil; | 3 | import cn.hutool.core.io.IoUtil; |
| 4 | import cn.hutool.extra.servlet.ServletUtil; | 4 | import cn.hutool.extra.servlet.ServletUtil; |
| 5 | -import cn.iocoder.dashboard.util.json.JSONUtils; | 5 | +import cn.iocoder.dashboard.util.json.JsonUtils; |
| 6 | import org.springframework.http.MediaType; | 6 | import org.springframework.http.MediaType; |
| 7 | import org.springframework.web.context.request.RequestAttributes; | 7 | import org.springframework.web.context.request.RequestAttributes; |
| 8 | import org.springframework.web.context.request.RequestContextHolder; | 8 | import org.springframework.web.context.request.RequestContextHolder; |
| @@ -28,7 +28,7 @@ public class ServletUtils { | @@ -28,7 +28,7 @@ public class ServletUtils { | ||
| 28 | */ | 28 | */ |
| 29 | @SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE,否则会乱码 | 29 | @SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE,否则会乱码 |
| 30 | public static void writeJSON(HttpServletResponse response, Object object) { | 30 | public static void writeJSON(HttpServletResponse response, Object object) { |
| 31 | - String content = JSONUtils.toJSONString(object); | 31 | + String content = JsonUtils.toJsonString(object); |
| 32 | ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE); | 32 | ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE); |
| 33 | } | 33 | } |
| 34 | 34 |