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 | 11 | import cn.iocoder.dashboard.framework.security.core.util.SecurityUtils; |
| 12 | 12 | import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; |
| 13 | 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 | 15 | import cn.iocoder.dashboard.util.servlet.ServletUtils; |
| 16 | 16 | import com.google.common.collect.Maps; |
| 17 | 17 | import io.swagger.annotations.Api; |
| ... | ... | @@ -319,7 +319,7 @@ public class OperateLogAspect { |
| 319 | 319 | // 被忽略时,标记为 ignore 字符串,避免和 null 混在一起 |
| 320 | 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 | 325 | private static String obtainResultData(Object result) { |
| ... | ... | @@ -327,7 +327,7 @@ public class OperateLogAspect { |
| 327 | 327 | if (result instanceof CommonResult) { |
| 328 | 328 | result = ((CommonResult<?>) result).getData(); |
| 329 | 329 | } |
| 330 | - return JSONUtils.toJSONString(result); | |
| 330 | + return JsonUtils.toJsonString(result); | |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 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 | 1 | package cn.iocoder.dashboard.framework.mybatis.core.type; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; |
| 4 | +import cn.iocoder.dashboard.util.json.JsonUtils; | |
| 4 | 5 | import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler; |
| 5 | -import com.fasterxml.jackson.core.JsonProcessingException; | |
| 6 | 6 | import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | -import com.fasterxml.jackson.databind.ObjectMapper; | |
| 8 | 7 | |
| 9 | -import java.io.IOException; | |
| 10 | 8 | import java.util.Set; |
| 11 | 9 | |
| 12 | 10 | /** |
| ... | ... | @@ -17,29 +15,18 @@ import java.util.Set; |
| 17 | 15 | * |
| 18 | 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 | 20 | private static final TypeReference<Set<Long>> typeReference = new TypeReference<Set<Long>>(){}; |
| 26 | 21 | |
| 27 | 22 | @Override |
| 28 | 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 | 27 | @Override |
| 37 | 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 | 1 | package cn.iocoder.dashboard.framework.redis.core.pubsub; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.ArrayUtil; |
| 4 | -import cn.iocoder.dashboard.util.json.JSONUtils; | |
| 4 | +import cn.iocoder.dashboard.util.json.JsonUtils; | |
| 5 | 5 | import lombok.SneakyThrows; |
| 6 | 6 | import org.springframework.data.redis.connection.Message; |
| 7 | 7 | import org.springframework.data.redis.connection.MessageListener; |
| ... | ... | @@ -44,7 +44,7 @@ public abstract class AbstractChannelMessageListener<T extends ChannelMessage> i |
| 44 | 44 | |
| 45 | 45 | @Override |
| 46 | 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 | 48 | this.onMessage(messageObj); |
| 49 | 49 | } |
| 50 | 50 | ... | ... |
src/main/java/cn/iocoder/dashboard/framework/redis/core/util/RedisMessageUtils.java
| 1 | 1 | package cn.iocoder.dashboard.framework.redis.core.util; |
| 2 | 2 | |
| 3 | 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 | 5 | import org.springframework.data.redis.core.RedisTemplate; |
| 6 | 6 | |
| 7 | 7 | /** |
| ... | ... | @@ -18,7 +18,7 @@ public class RedisMessageUtils { |
| 18 | 18 | * @param message 消息 |
| 19 | 19 | */ |
| 20 | 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 | 2 | |
| 3 | 3 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| 4 | 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 | 6 | import cn.iocoder.dashboard.framework.security.core.enums.DataScopeEnum; |
| 7 | 7 | import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum; |
| 8 | 8 | import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum; |
| ... | ... | @@ -71,7 +71,7 @@ public class SysRoleDO extends BaseDO { |
| 71 | 71 | * |
| 72 | 72 | * 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM} 时 |
| 73 | 73 | */ |
| 74 | - @TableField(typeHandler = JacksonLongSetTypeHandler.class) | |
| 74 | + @TableField(typeHandler = JsonLongSetTypeHandler.class) | |
| 75 | 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 | 2 | |
| 3 | 3 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| 4 | 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 | 6 | import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum; |
| 7 | 7 | import com.baomidou.mybatisplus.annotation.TableField; |
| 8 | 8 | import com.baomidou.mybatisplus.annotation.TableId; |
| ... | ... | @@ -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 | 58 | private Set<Long> postIds; |
| 59 | 59 | /** |
| 60 | 60 | * 用户邮箱 | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/dao/auth/SysLoginUserRedisDAO.java
| 1 | 1 | package cn.iocoder.dashboard.modules.system.dal.redis.dao.auth; |
| 2 | 2 | |
| 3 | 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 | 5 | import org.springframework.data.redis.core.StringRedisTemplate; |
| 6 | 6 | import org.springframework.stereotype.Repository; |
| 7 | 7 | |
| ... | ... | @@ -22,12 +22,12 @@ public class SysLoginUserRedisDAO { |
| 22 | 22 | |
| 23 | 23 | public LoginUser get(String sessionId) { |
| 24 | 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 | 28 | public void set(String sessionId, LoginUser loginUser) { |
| 29 | 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 | 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 | 1 | package cn.iocoder.dashboard.util.json; |
| 2 | 2 | |
| 3 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; | |
| 4 | 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | 6 | |
| 6 | 7 | import java.io.IOException; |
| 8 | +import java.util.Set; | |
| 7 | 9 | |
| 8 | 10 | /** |
| 9 | 11 | * JSON 工具类 |
| 10 | 12 | * |
| 11 | 13 | * @author 芋道源码 |
| 12 | 14 | */ |
| 13 | -public class JSONUtils { | |
| 15 | +public class JsonUtils { | |
| 14 | 16 | |
| 15 | 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 | 31 | try { |
| 24 | 32 | return objectMapper.writeValueAsString(object); |
| 25 | 33 | } catch (JsonProcessingException e) { |
| ... | ... | @@ -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 | 2 | |
| 3 | 3 | import cn.hutool.core.io.IoUtil; |
| 4 | 4 | import cn.hutool.extra.servlet.ServletUtil; |
| 5 | -import cn.iocoder.dashboard.util.json.JSONUtils; | |
| 5 | +import cn.iocoder.dashboard.util.json.JsonUtils; | |
| 6 | 6 | import org.springframework.http.MediaType; |
| 7 | 7 | import org.springframework.web.context.request.RequestAttributes; |
| 8 | 8 | import org.springframework.web.context.request.RequestContextHolder; |
| ... | ... | @@ -28,7 +28,7 @@ public class ServletUtils { |
| 28 | 28 | */ |
| 29 | 29 | @SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE,否则会乱码 |
| 30 | 30 | public static void writeJSON(HttpServletResponse response, Object object) { |
| 31 | - String content = JSONUtils.toJSONString(object); | |
| 31 | + String content = JsonUtils.toJsonString(object); | |
| 32 | 32 | ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE); |
| 33 | 33 | } |
| 34 | 34 | ... | ... |