提交 169e3dda 编写于 作者: 如梦技术's avatar 如梦技术 🐛

🐛 List Json log.

上级 ff8427e6
ext { ext {
javaVersion = JavaVersion.VERSION_1_8 javaVersion = JavaVersion.VERSION_1_8
springBootVersion = "2.1.10.RELEASE" springBootVersion = "2.1.11.RELEASE"
springCloudVersion = "Greenwich.SR4" springCloudVersion = "Greenwich.SR4"
springCloudAlibabaVersion = "2.1.1.RELEASE" springCloudAlibabaVersion = "2.1.1.RELEASE"
micaAutoVersion = "1.1.0" micaAutoVersion = "1.2.0"
apolloVersion = "1.4.0" apolloVersion = "1.4.0"
protostuffVersion = "1.6.0" protostuffVersion = "1.6.0"
disruptorVersion = "3.4.2" disruptorVersion = "3.4.2"
......
...@@ -16,10 +16,15 @@ ...@@ -16,10 +16,15 @@
package net.dreamlu.mica.servlet.logger; package net.dreamlu.mica.servlet.logger;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.*; import net.dreamlu.mica.core.utils.ClassUtil;
import net.dreamlu.mica.core.utils.JsonUtil;
import net.dreamlu.mica.core.utils.StringUtil;
import net.dreamlu.mica.core.utils.WebUtil;
import net.dreamlu.mica.launcher.MicaLogLevel; import net.dreamlu.mica.launcher.MicaLogLevel;
import net.dreamlu.mica.props.MicaRequestLogProperties; import net.dreamlu.mica.props.MicaRequestLogProperties;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
...@@ -39,7 +44,6 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -39,7 +44,6 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.*; import java.util.*;
...@@ -58,7 +62,6 @@ import java.util.concurrent.TimeUnit; ...@@ -58,7 +62,6 @@ import java.util.concurrent.TimeUnit;
@ConditionalOnProperty(value = MicaLogLevel.REQ_LOG_PROPS_PREFIX + ".enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(value = MicaLogLevel.REQ_LOG_PROPS_PREFIX + ".enabled", havingValue = "true", matchIfMissing = true)
public class RequestLogAspect { public class RequestLogAspect {
private final MicaRequestLogProperties properties; private final MicaRequestLogProperties properties;
private final ObjectMapper objectMapper;
/** /**
* AOP 环切 控制器 R 返回值 * AOP 环切 控制器 R 返回值
...@@ -95,13 +98,13 @@ public class RequestLogAspect { ...@@ -95,13 +98,13 @@ public class RequestLogAspect {
String parameterName = methodParam.getParameterName(); String parameterName = methodParam.getParameterName();
Object value = args[i]; Object value = args[i];
// 如果是body的json则是对象 // 如果是body的json则是对象
if (requestBody != null) { if (requestBody != null && value != null) {
if (value == null) { JsonNode jsonNode = JsonUtil.valueToTree(value);
paraMap.put(parameterName, null); if (JsonUtil.valueToTree(value) instanceof ObjectNode) {
} else if (ClassUtil.isPrimitiveOrWrapper(value.getClass())) { paraMap.putAll(JsonUtil.convertValue(jsonNode, new TypeReference<Map<? extends String, ?>>() {
paraMap.put(parameterName, value); }));
} else { } else {
paraMap.putAll(BeanUtil.toMap(value)); paraMap.put(parameterName, value);
} }
continue; continue;
} }
...@@ -135,7 +138,7 @@ public class RequestLogAspect { ...@@ -135,7 +138,7 @@ public class RequestLogAspect {
paraMap.put(paraName, "InputStream"); paraMap.put(paraName, "InputStream");
} else if (value instanceof InputStreamSource) { } else if (value instanceof InputStreamSource) {
paraMap.put(paraName, "InputStreamSource"); paraMap.put(paraName, "InputStreamSource");
} else if (canJsonSerialize(value)) { } else if (JsonUtil.canSerialize(value)) {
// 判断模型能被 json 序列化,则添加 // 判断模型能被 json 序列化,则添加
paraMap.put(paraName, value); paraMap.put(paraName, value);
} else { } else {
...@@ -201,12 +204,4 @@ public class RequestLogAspect { ...@@ -201,12 +204,4 @@ public class RequestLogAspect {
} }
} }
private boolean canJsonSerialize(Object value) {
try {
objectMapper.writeValueAsBytes(value);
return true;
} catch (IOException e) {
return false;
}
}
} }
...@@ -18,6 +18,7 @@ package net.dreamlu.mica.core.utils; ...@@ -18,6 +18,7 @@ package net.dreamlu.mica.core.utils;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.type.CollectionLikeType; import com.fasterxml.jackson.databind.type.CollectionLikeType;
...@@ -204,7 +205,7 @@ public class JsonUtil { ...@@ -204,7 +205,7 @@ public class JsonUtil {
* @return Bean * @return Bean
*/ */
@Nullable @Nullable
public static <T> T readValue(@Nullable byte[] content, TypeReference<?> typeReference) { public static <T> T readValue(@Nullable byte[] content, TypeReference<T> typeReference) {
if (ObjectUtil.isEmpty(content)) { if (ObjectUtil.isEmpty(content)) {
return null; return null;
} }
...@@ -224,7 +225,7 @@ public class JsonUtil { ...@@ -224,7 +225,7 @@ public class JsonUtil {
* @return Bean * @return Bean
*/ */
@Nullable @Nullable
public static <T> T readValue(@Nullable String jsonString, TypeReference<?> typeReference) { public static <T> T readValue(@Nullable String jsonString, TypeReference<T> typeReference) {
if (StringUtil.isBlank(jsonString)) { if (StringUtil.isBlank(jsonString)) {
return null; return null;
} }
...@@ -244,7 +245,7 @@ public class JsonUtil { ...@@ -244,7 +245,7 @@ public class JsonUtil {
* @return Bean * @return Bean
*/ */
@Nullable @Nullable
public static <T> T readValue(@Nullable InputStream in, TypeReference<?> typeReference) { public static <T> T readValue(@Nullable InputStream in, TypeReference<T> typeReference) {
if (in == null) { if (in == null) {
return null; return null;
} }
...@@ -396,6 +397,18 @@ public class JsonUtil { ...@@ -396,6 +397,18 @@ public class JsonUtil {
} }
} }
/**
* jackson 的类型转换
*
* @param fromValue 来源对象
* @param toValueType 转换的类型
* @param <T> 泛型标记
* @return 转换结果
*/
public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
return getInstance().convertValue(fromValue, toValueType);
}
/** /**
* jackson 的类型转换 * jackson 的类型转换
* *
...@@ -420,6 +433,45 @@ public class JsonUtil { ...@@ -420,6 +433,45 @@ public class JsonUtil {
return getInstance().convertValue(fromValue, toValueTypeRef); return getInstance().convertValue(fromValue, toValueTypeRef);
} }
/**
* tree 转对象
*
* @param treeNode TreeNode
* @param valueType valueType
* @param <T> 泛型标记
* @return 转换结果
*/
public static <T> T treeToValue(TreeNode treeNode, Class<T> valueType) {
try {
return getInstance().treeToValue(treeNode, valueType);
} catch (JsonProcessingException e) {
throw Exceptions.unchecked(e);
}
}
/**
* 对象转为 json node
*
* @param value 对象
* @return JsonNode
*/
public static JsonNode valueToTree(@Nullable Object value) {
return getInstance().valueToTree(value);
}
/**
* 判断是否可以序列化
*
* @param value 对象
* @return 是否可以序列化
*/
public static boolean canSerialize(@Nullable Object value) {
if (value == null) {
return true;
}
return getInstance().canSerialize(value.getClass());
}
public static ObjectMapper getInstance() { public static ObjectMapper getInstance() {
return JacksonHolder.INSTANCE; return JacksonHolder.INSTANCE;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册