change ali fast json to jackson

This commit is contained in:
JiyangTang 2024-08-06 13:33:16 +08:00
parent f24b569de1
commit 4e5d7b12a1
7 changed files with 187 additions and 26 deletions

View File

@ -1,7 +1,6 @@
package top.baogutang.admin.schedule;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.core.type.TypeReference;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
@ -16,6 +15,7 @@ import top.baogutang.admin.utils.DingTalkMsgPushUtils;
import top.baogutang.common.constants.NoticeTypeEnum;
import top.baogutang.common.domain.PageUtil;
import top.baogutang.common.domain.Results;
import top.baogutang.common.utils.JacksonUtil;
import top.baogutang.common.utils.OkHttpUtil;
import javax.annotation.Resource;
@ -46,7 +46,7 @@ public class NoticeScheduleHandler extends IJobHandler {
.forEach(noticeType -> {
Results<PageUtil<AnnouncementsDto>> results = OkHttpUtil.get(noticeType.getListUrl(), null, null, new TypeReference<Results<PageUtil<AnnouncementsDto>>>() {
});
log.info(">>>>>>>>>>请求获取:{}公告返回数据:{}<<<<<<<<<<", noticeType.getDesc(), JSON.toJSONString(results));
log.info(">>>>>>>>>>请求获取:{}公告返回数据:{}<<<<<<<<<<", noticeType.getDesc(), JacksonUtil.toJson(results));
if (Objects.isNull(results)) {
return;
}

View File

@ -1,6 +1,5 @@
package top.baogutang.admin.services.impl;
import com.alibaba.fastjson.JSON;
import com.zjiecode.wxpusher.client.WxPusher;
import com.zjiecode.wxpusher.client.bean.Message;
import com.zjiecode.wxpusher.client.bean.MessageResult;
@ -14,6 +13,7 @@ import top.baogutang.admin.services.IWxMsgPushService;
import com.zjiecode.wxpusher.client.bean.Page;
import top.baogutang.common.exceptions.BusinessException;
import top.baogutang.common.properties.WxMsgPushProperties;
import top.baogutang.common.utils.JacksonUtil;
import javax.annotation.Resource;
import java.util.Collections;
@ -54,7 +54,7 @@ public class WxMsgPushServiceImpl implements IWxMsgPushService {
log.error(">>>>>>>>>>推送消息异常:{}<<<<<<<<<<", e.getMessage(), e);
return Boolean.FALSE;
}
log.info(">>>>>>>>>>消息推送结果:{}<<<<<<<<<<", JSON.toJSONString(result));
log.info(">>>>>>>>>>消息推送结果:{}<<<<<<<<<<", JacksonUtil.toJson(result));
if (Objects.nonNull(result) && result.isSuccess()) {
return Boolean.TRUE;
}
@ -70,7 +70,7 @@ public class WxMsgPushServiceImpl implements IWxMsgPushService {
log.error(">>>>>>>>>>查询用户信息失败:{}<<<<<<<<<<", e.getMessage(), e);
throw new BusinessException("查询用户信息失败");
}
log.info(">>>>>>>>>>查询用户列表结果:{}<<<<<<<<<<", JSON.toJSONString(wxUsersResult));
log.info(">>>>>>>>>>查询用户列表结果:{}<<<<<<<<<<", JacksonUtil.toJson(wxUsersResult));
if (Objects.nonNull(wxUsersResult) && Objects.nonNull(wxUsersResult.getData()) && CollectionUtils.isNotEmpty(wxUsersResult.getData().getRecords())) {
return wxUsersResult.getData().getRecords().stream()
.map(WxUser::getUid)

View File

@ -1,6 +1,5 @@
package top.baogutang.admin.utils;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
@ -20,6 +19,7 @@ import top.baogutang.common.constants.DingTalkMsgTypeEnum;
import top.baogutang.common.exceptions.BusinessException;
import top.baogutang.common.properties.DingTalkConfigFactory;
import top.baogutang.common.properties.DingTalkConfigStrategy;
import top.baogutang.common.utils.JacksonUtil;
import javax.annotation.Resource;
import javax.crypto.Mac;
@ -90,7 +90,7 @@ public class DingTalkMsgPushUtils {
OapiMessageCorpconversationAsyncsendV2Response response = null;
try {
response = client.execute(request, accessToken);
log.info(">>>>>>>>>>request dingTalk send msg res:{}<<<<<<<<<<", JSON.toJSONString(response));
log.info(">>>>>>>>>>request dingTalk send msg res:{}<<<<<<<<<<", JacksonUtil.toJson(response));
return response.getTaskId();
} catch (Exception e) {
log.error(">>>>>>>>>>request dingTalk send msg error:{}<<<<<<<<<<", e.getMessage(), e);
@ -122,13 +122,13 @@ public class DingTalkMsgPushUtils {
OapiGettokenResponse response;
try {
response = client.execute(request);
log.info(">>>>>>>>>>request dingTalk gene accessToken,request:[{}] response:[{}]<<<<<<<<<<", JSON.toJSONString(request), JSON.toJSONString(response));
log.info(">>>>>>>>>>request dingTalk gene accessToken,request:[{}] response:[{}]<<<<<<<<<<", JacksonUtil.toJson(request), JacksonUtil.toJson(response));
if (Objects.nonNull(response) && Objects.nonNull(response.getAccessToken())) {
redisTemplate.opsForValue().set(cacheKey, response.getAccessToken(), 7000L, TimeUnit.SECONDS);
return response.getAccessToken();
}
} catch (Exception e) {
log.error(">>>>>>>>>>request dingTalk gene accessToken error! request:{},errorMsg:{}<<<<<<<<<<", JSON.toJSONString(request), e.getMessage(), e);
log.error(">>>>>>>>>>request dingTalk gene accessToken error! request:{},errorMsg:{}<<<<<<<<<<", JacksonUtil.toJson(request), e.getMessage(), e);
}
return null;
}
@ -158,7 +158,7 @@ public class DingTalkMsgPushUtils {
} catch (Exception e) {
log.error(">>>>>>>>>>robot msg send error:{}<<<<<<<<<<", e.getMessage(), e);
}
log.info(">>>>>>>>>>robot msg send request:{}, response:{}<<<<<<<<<<", JSON.toJSONString(request), JSON.toJSONString(response));
log.info(">>>>>>>>>>robot msg send request:{}, response:{}<<<<<<<<<<", JacksonUtil.toJson(request), JacksonUtil.toJson(response));
}
private String sign(Long timestamp) {

View File

@ -1,7 +1,5 @@
package top.baogutang.common.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
@ -13,6 +11,7 @@ import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.baogutang.common.domain.Results;
import top.baogutang.common.utils.JacksonUtil;
import javax.servlet.http.HttpServletRequest;
@ -48,15 +47,15 @@ public class LogAspect {
cost = System.currentTimeMillis() - startMills;
log.info("请求结束!本次请求耗时:{},url: {}, method: {}, params: {},user:{}, token: {}, 响应结果:{}",
cost, request.getRequestURL().toString(),
request.getMethod(), pjp.getArgs(), JSON.toJSONString(request.getAttribute("user")), request.getHeader("authorization"),
JSON.toJSONString(result, SerializerFeature.DisableCircularReferenceDetect));
request.getMethod(), pjp.getArgs(), JacksonUtil.toJson(request.getAttribute("user")), request.getHeader("authorization"),
JacksonUtil.toJson(result));
if (result instanceof Results) {
Results r = (Results) result;
r.setRid(requestId);
}
} catch (Exception e) {
log.error("请求异常!!!本次请求耗时:{},error:{},url: {}, method: {}, params: {},user:{}, token: {}", cost, e, request.getRequestURL().toString(),
request.getMethod(), pjp.getArgs(), JSON.toJSONString(request.getAttribute("user")), request.getHeader("token"));
request.getMethod(), pjp.getArgs(), JacksonUtil.toJson(request.getAttribute("user")), request.getHeader("token"));
throw e;
}
return result;

View File

@ -1,6 +1,5 @@
package top.baogutang.common.aspect;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.After;
@ -17,6 +16,7 @@ import top.baogutang.common.constants.CacheConstant;
import top.baogutang.common.domain.JwtBody;
import top.baogutang.common.domain.TokenCodeEnum;
import top.baogutang.common.exceptions.BusinessException;
import top.baogutang.common.utils.JacksonUtil;
import top.baogutang.common.utils.UserThreadLocal;
import javax.annotation.Resource;
@ -85,10 +85,10 @@ public class LoginRequiredAspect {
if (!token.equals(redisToken)) {
JwtBody redisBody = tokenComponent.parseToken(redisToken);
if (body.getIat() > redisBody.getIat()) {
log.info("newTokenCover,currentBody:{} redisBody:{}", JSON.toJSONString(body), JSON.toJSONString(redisBody));
log.info("newTokenCover,currentBody:{} redisBody:{}", JacksonUtil.toJson(body), JacksonUtil.toJson(redisBody));
redisTemplate.opsForValue().set(CacheConstant.TOKEN + userId, token);
} else {
log.info("FoundOldToken,currentBody:{} redisBody:{}", JSON.toJSONString(body), JSON.toJSONString(redisBody));
log.info("FoundOldToken,currentBody:{} redisBody:{}", JacksonUtil.toJson(body), JacksonUtil.toJson(redisBody));
throw new BusinessException(
TokenCodeEnum.AUTH_TIME_OUT.getCode(), TokenCodeEnum.AUTH_TIME_OUT.getMessage());
}

View File

@ -0,0 +1,163 @@
package top.baogutang.common.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @description: JacksonUtil
* @author: nikooh
* @date: 2024/08/06 : 10:50
*/
@Slf4j
public class JacksonUtil {
private JacksonUtil() {
// empty private constructor
}
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
static {
// 所有的日期都统一用yyyy-MM-dd HH:mm:ss格式
OBJECT_MAPPER.setDateFormat(new SimpleDateFormat(DEFAULT_DATE_FORMAT));
// 忽略字符串存在对象不存在的属性
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
/**
* 对象转json字符串
*
* @param content 需要转换的对象
* @return json格式字符串
*/
public static String toJson(Object content) {
String result = StringUtils.EMPTY;
if (Objects.isNull(content)) {
return result;
}
try {
result = OBJECT_MAPPER.writeValueAsString(content);
} catch (Exception e) {
log.error(">>>>>>>>>>parse object to json fail:{}<<<<<<<<<<", e.getMessage());
}
return result;
}
/**
* 字符串转自定义对象
*
* @param content 需要转换的字符串
* @param clazz 需要转换后的类型
* @param <T> 泛型
* @return 转换后对象
*/
public static <T> T fromJson(String content, Class<T> clazz) {
T result = null;
if (StringUtils.isBlank(content)) {
return result;
}
try {
result = OBJECT_MAPPER.readValue(content, clazz);
} catch (Exception e) {
log.error(">>>>>>>>>>parse json to object fail:{}<<<<<<<<<<", e.getMessage());
}
return result;
}
/**
* 字符串转自定义对象
*
* @param content 需要转换的字符串
* @param type 需要转换后的类型
* @param <T> 泛型
* @return 转换后对象
*/
public static <T> T fromJson(String content, TypeReference<T> type) {
T result = null;
if (StringUtils.isBlank(content)) {
return result;
}
try {
result = OBJECT_MAPPER.readValue(content, type);
} catch (Exception e) {
log.error(">>>>>>>>>>parse json to object fail:{}<<<<<<<<<<", e.getMessage());
}
return result;
}
/**
* json字符串转换为List
*
* @param content 需要转换的字符串
* @param clazz 需要转换后的类型
* @param <T> 泛型
* @return 转换后的集合
*/
public static <T> List<T> jsonToList(String content, Class<T> clazz) {
List<T> list = new ArrayList<>();
if (StringUtils.isBlank(content) || clazz == null) {
return list;
}
try {
list = OBJECT_MAPPER.readValue(content, new TypeReference<List<T>>() {
});
} catch (Exception e) {
log.error(">>>>>>>>>>parse json to list fail:{}<<<<<<<<<<", e.getMessage());
}
return list;
}
/**
* json字符串转换为Map
*
* @param content 需要转换的字符串
* @param keyType 需要转换后的key类型
* @param valueType 需要转换后的value类型
* @param <K> Class<K>
* @param <V> Class<V>
* @return 转换后的Map
*/
public static <K, V> Map<K, V> jsonToMap(String content, Class<K> keyType, Class<V> valueType) {
Map<K, V> result = new HashMap<>();
if (StringUtils.isBlank(content) || Objects.isNull(keyType) || Objects.isNull(valueType)) {
return result;
}
try {
result = OBJECT_MAPPER.readValue(content, new TypeReference<Map<K, V>>() {
});
} catch (Exception e) {
log.error(">>>>>>>>>>parse json to map fail:{}<<<<<<<<<<", e.getMessage());
}
return result;
}
/**
* json字符串转换为JsonNode
*
* @param content 需要转换的字符串
* @return 转换后的JsonNode
*/
public static JsonNode jsonToNode(String content) {
JsonNode jsonNode = null;
if (StringUtils.isBlank(content)) {
return null;
}
try {
jsonNode = OBJECT_MAPPER.readTree(content);
} catch (Exception e) {
log.error(">>>>>>>>>>parse json to node fail:{}<<<<<<<<<<", e.getMessage());
}
return jsonNode;
}
}

View File

@ -1,7 +1,6 @@
package top.baogutang.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang.StringUtils;
@ -57,7 +56,7 @@ public class OkHttpUtil {
if (response.isSuccessful()) {
String content = Objects.requireNonNull(response.body()).string();
if (StringUtils.isNotBlank(content)) {
return JSON.parseObject(content, type);
return JacksonUtil.fromJson(content, type);
}
} else {
log.error("postRequest fail ,url:{}, params:{}, res:{}", url, params, response);
@ -74,14 +73,14 @@ public class OkHttpUtil {
if (Objects.nonNull(headerMap) && !headerMap.isEmpty()) {
headerMap.forEach(headerBuilder::add);
}
RequestBody body = RequestBody.create(JSON.toJSONString(params), MediaType.parse("application/json; charset=utf-8"));
RequestBody body = RequestBody.create(JacksonUtil.toJson(params), MediaType.parse("application/json; charset=utf-8"));
Request request = new Request.Builder().post(body).headers(headerBuilder.build()).url(url).build();
Response response = OkHttpUtil.getInstance().newCall(request).execute();
log.info("postJSONRequestReq:{},Res:{}, ", JSON.toJSONString(params), JSON.toJSONString(response));
log.info("postJSONRequestReq:{},Res:{}, ", JacksonUtil.toJson(params), JacksonUtil.toJson(response));
if (response.isSuccessful()) {
String content = Objects.requireNonNull(response.body()).string();
if (StringUtils.isNotBlank(content)) {
return JSON.parseObject(content, type);
return JacksonUtil.fromJson(content, type);
}
} else {
log.error("postJSONRequest fail ,url:{}, params:{}, res:{}", url, params, response);
@ -113,7 +112,7 @@ public class OkHttpUtil {
if (response.isSuccessful()) {
String content = Objects.requireNonNull(response.body()).string();
if (StringUtils.isNotBlank(content)) {
return JSON.parseObject(content, type);
return JacksonUtil.fromJson(content, type);
}
} else {
log.error("getRequest fail ,url:{}, params:{}, res:{}", url, params, response);