进一步优化 json 工具类,默认使用 Spring 初始化出来的
This commit is contained in:
parent
eadc4f749a
commit
fa3f382210
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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) {
|
||||
|
@ -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<Object> {
|
||||
|
||||
// TODO 芋艿,需要将 Spring 的设置下进来
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
public class JsonLongSetTypeHandler extends AbstractJsonTypeHandler<Object> {
|
||||
|
||||
private static final TypeReference<Set<Long>> typeReference = new TypeReference<Set<Long>>(){};
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
@ -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<T extends ChannelMessage> 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);
|
||||
}
|
||||
|
||||
|
@ -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 <T extends ChannelMessage> void sendChannelMessage(RedisTemplate<?, ?> redisTemplate, T message) {
|
||||
redisTemplate.convertAndSend(message.getChannel(), JSONUtils.toJSONString(message));
|
||||
redisTemplate.convertAndSend(message.getChannel(), JsonUtils.toJsonString(message));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<Long> dataScopeDeptIds;
|
||||
|
||||
}
|
||||
|
@ -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<Long> postIds;
|
||||
/**
|
||||
* 用户邮箱
|
||||
|
@ -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) {
|
||||
|
@ -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<Set<Long>> typeReference) {
|
||||
try {
|
||||
return objectMapper.readValue(text, typeReference);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user