mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 23:31:52 +08:00
进一步优化 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.security.core.util.SecurityUtils;
|
||||||
import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
|
import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
|
||||||
import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogCreateReqVO;
|
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 cn.iocoder.dashboard.util.servlet.ServletUtils;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
@ -319,7 +319,7 @@ public class OperateLogAspect {
|
|||||||
// 被忽略时,标记为 ignore 字符串,避免和 null 混在一起
|
// 被忽略时,标记为 ignore 字符串,避免和 null 混在一起
|
||||||
args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]");
|
args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]");
|
||||||
}
|
}
|
||||||
return JSONUtils.toJSONString(args);
|
return JsonUtils.toJsonString(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String obtainResultData(Object result) {
|
private static String obtainResultData(Object result) {
|
||||||
@ -327,7 +327,7 @@ public class OperateLogAspect {
|
|||||||
if (result instanceof CommonResult) {
|
if (result instanceof CommonResult) {
|
||||||
result = ((CommonResult<?>) result).getData();
|
result = ((CommonResult<?>) result).getData();
|
||||||
}
|
}
|
||||||
return JSONUtils.toJSONString(result);
|
return JsonUtils.toJsonString(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isIgnoreArgs(Object object) {
|
private static boolean isIgnoreArgs(Object object) {
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
package cn.iocoder.dashboard.framework.mybatis.core.type;
|
package cn.iocoder.dashboard.framework.mybatis.core.type;
|
||||||
|
|
||||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
|
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.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,29 +15,18 @@ import java.util.Set;
|
|||||||
*
|
*
|
||||||
* @author 芋道源码
|
* @author 芋道源码
|
||||||
*/
|
*/
|
||||||
public class JacksonLongSetTypeHandler extends AbstractJsonTypeHandler<Object> {
|
public class JsonLongSetTypeHandler extends AbstractJsonTypeHandler<Object> {
|
||||||
|
|
||||||
// TODO 芋艿,需要将 Spring 的设置下进来
|
|
||||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
|
|
||||||
private static final TypeReference<Set<Long>> typeReference = new TypeReference<Set<Long>>(){};
|
private static final TypeReference<Set<Long>> typeReference = new TypeReference<Set<Long>>(){};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object parse(String json) {
|
protected Object parse(String json) {
|
||||||
try {
|
return JsonUtils.parseObject(json, typeReference);
|
||||||
return objectMapper.readValue(json, typeReference);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String toJson(Object obj) {
|
protected String toJson(Object obj) {
|
||||||
try {
|
return JsonUtils.toJsonString(obj);
|
||||||
return objectMapper.writeValueAsString(obj);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package cn.iocoder.dashboard.framework.redis.core.pubsub;
|
package cn.iocoder.dashboard.framework.redis.core.pubsub;
|
||||||
|
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.iocoder.dashboard.util.json.JSONUtils;
|
import cn.iocoder.dashboard.util.json.JsonUtils;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.springframework.data.redis.connection.Message;
|
import org.springframework.data.redis.connection.Message;
|
||||||
import org.springframework.data.redis.connection.MessageListener;
|
import org.springframework.data.redis.connection.MessageListener;
|
||||||
@ -44,7 +44,7 @@ public abstract class AbstractChannelMessageListener<T extends ChannelMessage> i
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void onMessage(Message message, byte[] bytes) {
|
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);
|
this.onMessage(messageObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package cn.iocoder.dashboard.framework.redis.core.util;
|
package cn.iocoder.dashboard.framework.redis.core.util;
|
||||||
|
|
||||||
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
|
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;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,7 +18,7 @@ public class RedisMessageUtils {
|
|||||||
* @param message 消息
|
* @param message 消息
|
||||||
*/
|
*/
|
||||||
public static <T extends ChannelMessage> void sendChannelMessage(RedisTemplate<?, ?> redisTemplate, T 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.common.enums.CommonStatusEnum;
|
||||||
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
|
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.framework.security.core.enums.DataScopeEnum;
|
||||||
import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum;
|
import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum;
|
||||||
import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum;
|
import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum;
|
||||||
@ -71,7 +71,7 @@ public class SysRoleDO extends BaseDO {
|
|||||||
*
|
*
|
||||||
* 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM} 时
|
* 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM} 时
|
||||||
*/
|
*/
|
||||||
@TableField(typeHandler = JacksonLongSetTypeHandler.class)
|
@TableField(typeHandler = JsonLongSetTypeHandler.class)
|
||||||
private Set<Long> dataScopeDeptIds;
|
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.common.enums.CommonStatusEnum;
|
||||||
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
|
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 cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
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;
|
private Set<Long> postIds;
|
||||||
/**
|
/**
|
||||||
* 用户邮箱
|
* 用户邮箱
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package cn.iocoder.dashboard.modules.system.dal.redis.dao.auth;
|
package cn.iocoder.dashboard.modules.system.dal.redis.dao.auth;
|
||||||
|
|
||||||
import cn.iocoder.dashboard.framework.security.core.LoginUser;
|
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.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@ -22,12 +22,12 @@ public class SysLoginUserRedisDAO {
|
|||||||
|
|
||||||
public LoginUser get(String sessionId) {
|
public LoginUser get(String sessionId) {
|
||||||
String redisKey = formatKey(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) {
|
public void set(String sessionId, LoginUser loginUser) {
|
||||||
String redisKey = formatKey(sessionId);
|
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) {
|
public void delete(String accessToken) {
|
||||||
|
@ -1,25 +1,33 @@
|
|||||||
package cn.iocoder.dashboard.util.json;
|
package cn.iocoder.dashboard.util.json;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON 工具类
|
* JSON 工具类
|
||||||
*
|
*
|
||||||
* @author 芋道源码
|
* @author 芋道源码
|
||||||
*/
|
*/
|
||||||
public class JSONUtils {
|
public class JsonUtils {
|
||||||
|
|
||||||
private static ObjectMapper objectMapper = new ObjectMapper();
|
private static ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
// TODO 芋艿,需要将 Spring 的设置下进来
|
/**
|
||||||
public static void setObjectMapper(ObjectMapper objectMapper) {
|
* 初始化 objectMapper 属性
|
||||||
JSONUtils.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 {
|
try {
|
||||||
return objectMapper.writeValueAsString(object);
|
return objectMapper.writeValueAsString(object);
|
||||||
} catch (JsonProcessingException e) {
|
} 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.core.io.IoUtil;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
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.http.MediaType;
|
||||||
import org.springframework.web.context.request.RequestAttributes;
|
import org.springframework.web.context.request.RequestAttributes;
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
@ -28,7 +28,7 @@ public class ServletUtils {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE,否则会乱码
|
@SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE,否则会乱码
|
||||||
public static void writeJSON(HttpServletResponse response, Object object) {
|
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);
|
ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user