Commit 97dd0710 authored by yanzg's avatar yanzg

异常处理显示

parent 88b98773
package com.yanzuoguang.cloud.aop; package com.yanzuoguang.cloud.aop;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.yanzuoguang.cloud.CloudContans;
import com.yanzuoguang.util.cache.MemoryCache; import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.contants.ResultConstants; import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
...@@ -91,17 +90,7 @@ public class BaseRequestAspect implements ThreadNext.Next { ...@@ -91,17 +90,7 @@ public class BaseRequestAspect implements ThreadNext.Next {
logInterVo.setLogSources(applicationName); logInterVo.setLogSources(applicationName);
//请求URL //请求URL
logInterVo.setInterUrl(url); logInterVo.setInterUrl(url);
List<Object> para = new ArrayList<>(); Object paraTo = getFirstDataParameter(joinPoint.getArgs());
for (Object item : joinPoint.getArgs()) {
if (item instanceof HttpServlet || item instanceof HttpServletResponse || item instanceof HttpServletRequest) {
continue;
}
para.add(item);
}
Object paraTo = para;
if (para.size() == 1) {
paraTo = para.get(0);
}
//请求参数 //请求参数
logInterVo.setContent(JSON.toJSONString(paraTo)); logInterVo.setContent(JSON.toJSONString(paraTo));
//返回参数 //返回参数
...@@ -110,6 +99,37 @@ public class BaseRequestAspect implements ThreadNext.Next { ...@@ -110,6 +99,37 @@ public class BaseRequestAspect implements ThreadNext.Next {
return logInterVo; return logInterVo;
} }
/**
* 获取请求的参数
*
* @param args
* @return
*/
protected Object getFirstDataParameter(Object[] args) {
List<Object> para = getDataParameters(args);
Object paraTo = para;
if (para.size() == 1) {
paraTo = para.get(0);
}
return paraTo;
}
/**
* 获取数据参数
* @param args
* @return
*/
protected List<Object> getDataParameters(Object[] args) {
List<Object> para = new ArrayList<>();
for (Object item : args) {
if (item instanceof HttpServlet || item instanceof HttpServletResponse || item instanceof HttpServletRequest) {
continue;
}
para.add(item);
}
return para;
}
/** /**
* 获取JSON,当Json过长时,截断 * 获取JSON,当Json过长时,截断
* *
...@@ -147,7 +167,7 @@ public class BaseRequestAspect implements ThreadNext.Next { ...@@ -147,7 +167,7 @@ public class BaseRequestAspect implements ThreadNext.Next {
try { try {
String name = joinPoint.getSignature().getName(); String name = joinPoint.getSignature().getName();
Log.info(joinPoint.getSignature().getDeclaringType(), " %s [ %s ] request: %s", Log.info(joinPoint.getSignature().getDeclaringType(), " %s [ %s ] request: %s",
tag, name, this.getMaxString(JsonHelper.serialize(joinPoint.getArgs()))); tag, name, this.getMaxString(JsonHelper.serialize(getFirstDataParameter(joinPoint.getArgs()))));
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
......
...@@ -16,10 +16,9 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -16,10 +16,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List;
/** /**
* LogsAspect(接口请求日志切面) * LogsAspect(接口请求日志切面)
...@@ -148,9 +147,8 @@ public class WebAspect extends BaseRequestAspect { ...@@ -148,9 +147,8 @@ public class WebAspect extends BaseRequestAspect {
* @throws Throwable * @throws Throwable
*/ */
private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable { private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
boolean dataArgs = joinPoint.getArgs().length != 1 List<Object> dataParas = this.getDataParameters(joinPoint.getArgs());
|| joinPoint.getArgs().length == 1 && boolean dataArgs = dataParas.isEmpty();
(joinPoint.getArgs()[0] instanceof ServletResponse || joinPoint.getArgs()[0] instanceof ServletRequest);
Method method = getMethod(joinPoint); Method method = getMethod(joinPoint);
boolean isUrl = false; boolean isUrl = false;
if (!StringHelper.isEmpty(reqUrl)) { if (!StringHelper.isEmpty(reqUrl)) {
...@@ -166,7 +164,7 @@ public class WebAspect extends BaseRequestAspect { ...@@ -166,7 +164,7 @@ public class WebAspect extends BaseRequestAspect {
return joinPoint.proceed(); return joinPoint.proceed();
} else { } else {
// 获取请求编号 // 获取请求编号
Object firstArgs = joinPoint.getArgs().length > 0 ? joinPoint.getArgs()[0] : null; Object firstArgs = dataParas.size() > 0 ? dataParas.get(0) : null;
String reqId = StringHelper.md5(JsonHelper.serialize(firstArgs)); String reqId = StringHelper.md5(JsonHelper.serialize(firstArgs));
String reqFull = StringHelper.getId(reqId, HttpAspectUtil.getHttpRequestUrl()); String reqFull = StringHelper.getId(reqId, HttpAspectUtil.getHttpRequestUrl());
RequestCacheResult req = cacheResult.get(reqFull, new RequestCacheResult(reqFull)); RequestCacheResult req = cacheResult.get(reqFull, new RequestCacheResult(reqFull));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment