1、背景
开发个小功能,下载Excel模版
代码写好后,下测试了下,发现报错:getOutputStream() has already been called for this response
开始排查问题
主要是通过看报错日志,发现代码框架里有个日志的切面
2、原因分析
主要原因是LoggerAspect类里使用反射机制,给所有的controller都加了环绕日志
原来代码:
package com.xxx.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.bouncycastle.asn1.ocsp.ResponseData;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Aspect
@Component
@Slf4j
@Order(0)
public class LoggerAspect {
@Pointcut("execution(public * com.xxx.controller..*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void aspect() {
}
@Around(value = "aspect()")
public Object loggerAspect(ProceedingJoinPoint pjp) throws Throwable {
//记录请求开始执行时间:
long beginTime = System.currentTimeMillis();
//获取请求信息
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = sra.getRequest();
//请求地址、请求类名、方法名
String requestURI = request.getRequestURI();
String methodName = pjp.getSignature().getName();
String clazzName = pjp.getTarget().getClass().getSimpleName();
//获取请求参数:
MethodSignature ms = (MethodSignature) pjp.getSignature();
//获取请求参数类型
String[] parameterNames = ms.getParameterNames();
//获取请求参数值
Object[] parameterValues = pjp.getArgs();
log.info("请求requestURI={},入参={}",requestURI, JSONObject.toJSONString(parameterValues));
StringBuilder sb = new StringBuilder();
//组合请求参数,进行日志打印
if (parameterNames != null && parameterNames.length > 0) {
for (int i = 0; i < parameterNames.length; i++) {
if (parameterNames[i].equals("bindingResult")) {
break;
}
if ((parameterValues[i] instanceof HttpServletRequest) || (parameterValues[i] instanceof HttpServletResponse)) {
sb.
append("[").
append(parameterNames[i]).append("=").append(parameterValues[i])
.append("]");
} else {
sb.
append("[").
append(parameterNames[i]).append("=")
.append(JSON.toJSONString(parameterValues[i], SerializerFeature.WriteDateUseDateFormat))
.append("]");
}
}
}
Object result = null;
try {
result = pjp.proceed();
} catch (Throwable throwable) {
//请求操纵失败
//记录错误日志
log.error("(ง•̀_•́)ง (っ•̀ω•́)っ 切面处理请求错误!" +
"URI信息(ง•̀_•́)ง->:【{}】 请求映射控制类(ง•̀_•́)ง->:【{}】 " +
"请求方法(ง•̀_•́)́́́ง->:【{}】 请求参数列表(ง•̀_•́)ง->:【{}】", requestURI, clazzName, methodName,
sb.toString());
throw throwable;
}
//请求操作成功
String resultJosnString = "";
if (result != null) {
if (result instanceof ResponseData) {
resultJosnString = JSON.toJSONString(result, SerializerFeature.WriteDateUseDateFormat);
} else {
resultJosnString = String.valueOf(result);
}
}
//记录请求完成执行时间:
long endTime = System.currentTimeMillis();
long usedTime = endTime - beginTime;
//记录日志
log.info("请求操作成功! 请求耗时:【{}】 " +
"URI信息(◍'౪`◍)ノ゙->:【{}】 " +
"请求映射控制类(◍'౪`◍)ノ゙->:【{}】 请求方法(◍'౪`◍)ノ゙->:【{}】 " +
"请求参数列表(◍'౪`◍)ノ゙->:【{}】 返回值(◍'౪`◍)ノ゙->:【{}】", usedTime, requestURI, clazzName,
methodName, sb.toString(), resultJosnString);
return result;
}
}
修改后代码:
package com.xxx.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.bouncycastle.asn1.ocsp.ResponseData;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Aspect
@Component
@Slf4j
@Order(0)
public class LoggerAspect {
@Pointcut("execution(public * com.xxx.controller..*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void aspect() {
}
@Pointcut("execution(public * com.xxx.controller.XxxController.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void excludeResponse() {
}
@Around(value = "aspect() && !excludeResponse()")
public Object loggerAspect(ProceedingJoinPoint pjp) throws Throwable {
//记录请求开始执行时间:
long beginTime = System.currentTimeMillis();
//获取请求信息
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = sra.getRequest();
//请求地址、请求类名、方法名
String requestURI = request.getRequestURI();
String methodName = pjp.getSignature().getName();
String clazzName = pjp.getTarget().getClass().getSimpleName();
//获取请求参数:
MethodSignature ms = (MethodSignature) pjp.getSignature();
//获取请求参数类型
String[] parameterNames = ms.getParameterNames();
//获取请求参数值
Object[] parameterValues = pjp.getArgs();
log.info("请求requestURI={},入参={}",requestURI, JSONObject.toJSONString(parameterValues));
StringBuilder sb = new StringBuilder();
//组合请求参数,进行日志打印
if (parameterNames != null && parameterNames.length > 0) {
for (int i = 0; i < parameterNames.length; i++) {
if (parameterNames[i].equals("bindingResult")) {
break;
}
if ((parameterValues[i] instanceof HttpServletRequest) || (parameterValues[i] instanceof HttpServletResponse)) {
sb.
append("[").
append(parameterNames[i]).append("=").append(parameterValues[i])
.append("]");
} else {
sb.
append("[").
append(parameterNames[i]).append("=")
.append(JSON.toJSONString(parameterValues[i], SerializerFeature.WriteDateUseDateFormat))
.append("]");
}
}
}
Object result = null;
try {
result = pjp.proceed();
} catch (Throwable throwable) {
//请求操纵失败
//记录错误日志
log.error("(ง•̀_•́)ง (っ•̀ω•́)っ 切面处理请求错误!" +
"URI信息(ง•̀_•́)ง->:【{}】 请求映射控制类(ง•̀_•́)ง->:【{}】 " +
"请求方法(ง•̀_•́)́́́ง->:【{}】 请求参数列表(ง•̀_•́)ง->:【{}】", requestURI, clazzName, methodName,
sb.toString());
throw throwable;
}
//请求操作成功
String resultJosnString = "";
if (result != null) {
if (result instanceof ResponseData) {
resultJosnString = JSON.toJSONString(result, SerializerFeature.WriteDateUseDateFormat);
} else {
resultJosnString = String.valueOf(result);
}
}
//记录请求完成执行时间:
long endTime = System.currentTimeMillis();
long usedTime = endTime - beginTime;
//记录日志
log.info("请求操作成功! 请求耗时:【{}】 " +
"URI信息(◍'౪`◍)ノ゙->:【{}】 " +
"请求映射控制类(◍'౪`◍)ノ゙->:【{}】 请求方法(◍'౪`◍)ノ゙->:【{}】 " +
"请求参数列表(◍'౪`◍)ノ゙->:【{}】 返回值(◍'౪`◍)ノ゙->:【{}】", usedTime, requestURI, clazzName,
methodName, sb.toString(), resultJosnString);
return result;
}
}
将返回response的进行了过滤,成功解决问题!
另外,上面的日志切面代码是存在一定局限性的,不能满足所有使用场景!