本文主要是介绍getOutputStream() has already been called for this response,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题描述
在做java导出Excel数据的时候,接口层面需要有HttpServletResponse的入参来设置输出流
然后执行的时候报getOutputStream() has already been called for this response错误
问题排查
返回的错误信息
{"timestamp": "2024-04-16T11:49:54.900+00:00","status": 500,"error": "Internal Server Error","message": "getOutputStream() has already been called for this response","path": "/export/exportData"
}
查询错误日志
java.lang.IllegalStateException: getOutputStream() has already been called for this responseat org.apache.catalina.connector.Response.getWriter(Response.java:586)at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:225)at com.alibaba.fastjson.serializer.ASMSerializer_5_ResponseFacade.write(Unknown Source)at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:312)at com.alibaba.fastjson.JSON.toJSONString(JSON.java:793)at com.alibaba.fastjson.JSON.toJSONString(JSON.java:731)at com.alibaba.fastjson.JSON.toJSONString(JSON.java:688)at com.XXX.open.record.aspect.ServiceAspect.before(ServiceAspect.java:54)at sun.reflect.GeneratedMethodAccessor296.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:626)at org.springframework.aop.aspectj.AspectJMethodBeforeAdvice.before(AspectJMethodBeforeAdvice.java:44)at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:55)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
从错误日志可以发现是在切面ServiceAspect类的before方法报错的
点击进入报错的代码,发现此处的方式是
@Before(value = POINT_CUT)public void before(JoinPoint joinPoint) {String className = joinPoint.getTarget().getClass().getName();String methodName = joinPoint.getSignature().getName();StringBuilder logStr = new StringBuilder();logStr.append(className).append(".").append(methodName).append("() , params: ");Object[] args = joinPoint.getArgs();for (Object arg : args) {logStr.append(JSONObject.toJSONString(arg) + ", ");//报错代码}RequestContext requestContext = RequestContext.getCurrentContext();requestContext.set(ConfigConstants.REQUEST_DATA, logStr.toString());// 通过Sleuth的Tracer获取本次请求的唯一标识,并作为logId打印if (null != tracer && null != tracer.currentSpan()) {requestContext.set(RequestParamConst.LOG_ID, tracer.currentSpan().context().traceIdString());} else {requestContext.set(RequestParamConst.LOG_ID, "");}
看来是json序列化HttpServletResponse参数时报错的。
解决方法
添加判断,如果入参类型是HttpServletResponse,则不序列号。问题解决
@Before(value = POINT_CUT)public void before(JoinPoint joinPoint) {String className = joinPoint.getTarget().getClass().getName();String methodName = joinPoint.getSignature().getName();StringBuilder logStr = new StringBuilder();logStr.append(className).append(".").append(methodName).append("() , params: ");Object[] args = joinPoint.getArgs();for (Object arg : args) {if(arg instanceof HttpServletResponse){continue;}logStr.append(JSONObject.toJSONString(arg) + ", ");}RequestContext requestContext = RequestContext.getCurrentContext();requestContext.set(ConfigConstants.REQUEST_DATA, logStr.toString());// 通过Sleuth的Tracer获取本次请求的唯一标识,并作为logId打印if (null != tracer && null != tracer.currentSpan()) {requestContext.set(RequestParamConst.LOG_ID, tracer.currentSpan().context().traceIdString());} else {requestContext.set(RequestParamConst.LOG_ID, "");}}
这篇关于getOutputStream() has already been called for this response的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!