getOutputStream() has already been called for this response

2024-04-17 13:12

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/911889

相关文章

Tomcat启动报错:transport error 202: bind failed: Address already in use

Tomcat启动报错:transport error 202: bind failed: Address already in use 了,上网查找了下面这篇文章。也是一种解决办法。 下文来自:http://blog.csdn.net/sam031503/article/details/7037033 tomcat 启动日志报出以下错误:  ERROR: transport err

兔子-在as中删除项目以及删除后报错already disposed:Module:'haha'

1. 2.点左上角红色的减号,,Apply 3,删除setting.gradle中include中的要删除的项目 . 4.选择要删除的项目,,点击键盘的delete键 5.点击第二个图标。 删除成功!! 完全删除haha项目之后,仍然报这个错误。 解决办法:重启as

ZeroMQ(java)之Requerst/Response模式

自己最开始是在cloud foundry中接触过消息服务器(nats),或者说是消息中间件,也算是初步知道了一个消息服务器对于分布式的网络系统的重要性,后来自己也曾想过在一些项目中使用它,尤其是在一些分布式的环境下,可以极大的方便整个系统的实现。。。。 例如如下的形式: 在中间通过一个消息中间件,可以很方便的将各个woker的数据发送到最终的统计服务器来做数据的统计,从而

Address localhost:1099 is already in use:tomcat频繁重启端口占用问题

错误提示 Unable to open debugger port (127.0.0.1:58198): java.net.SocketException "Socket closed" Address localhost:1099 is already in use 端口被占用 报错原因 由于短时间内频繁运行tomcat服务器。 为了避免出现这一错误。可以点击刷新uodate

【Puppeteer】‘left‘ is already pressed, ‘${button}‘ is already pressed 的解决办法

解决过程如下 这是我原来的代码,不管我怎么修改,都一直会出现 'left' is already pressed 这个错误 找了很多资料 搜了 很多网站都 找不到解决办法 async function dragAndDrop(page, canvasSelector, startX, startY, endX, endY) {const startCoordinates = await ge

缓解webclient频繁报‘Connection prematurely closed BEFORE response’的问题

现象: 我在Java代码中使用org.springframework.web.reactive.function.client.WebClient进行网络请求,一开始会有比较多的偶发报错:Connection prematurely closed BEFORE response,网络连接莫名其妙就断了。 处理: 在网上找了挺多资料,就感觉https://stackoverflow.com/q

spark-shell启动报错:Yarn application has already ended! It might have been killed or unable to launch...

前半部分转自:https://www.cnblogs.com/tibit/p/7337045.html (后半原创) spark-shell不支持yarn cluster,以yarn client方式启动 spark-shell --master=yarn --deploy-mode=client 启动日志,错误信息如下   其中“Neither spark.yarn.ja

手写服务器httpserver_封装Response_封装Request_JAVA199-201

来源:http://www.bjsxt.com/ 一、S02E199_01手写服务器httpserver_封装Response <html><head><title>第一个表单</title></head><body><pre>method:请求方式 get/postget:默认方式,数据量小,安全性不高post:量大,安全性相对高action:请求的服务器路径id:编号,前端(用户的浏览器)

The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method 已解决

前面有个webSocket自动断开连接的问题,已解决,请见博客: webSocket java.io.EOFException: null 增加心跳机制解决 然后又报了一个错: java.lang.IllegalStateException: The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid st

Error:(106) Attribute quot;rippleColorquot; has already been defined

compile 'com.github.navasmdc:MaterialDesign:1.5@aar' 扁平化设计,导入出现: Error:(106) Attribute "rippleColor" has already been defined 解决: compile 'com.github.navasmdc:MaterialDesign:1.3.2'