我正在尝试尽可能安静地处理客户端关闭的入站连接,即。 e.不向应用程序日志发送垃圾邮件
ERROR
消息和堆栈跟踪,而仅记录简短的 WARN
消息。
我在 Spring Boot 应用程序中使用 Jetty 作为嵌入式服务器。默认情况下,关闭的连接会导致此错误:
java.io.IOException: Close SendCallback@7cd49aa0[PROCESSING][i=HTTP/1.1{s=200,h=16,cl=-1},cb=org.eclipse.jetty.server.HttpChannel$SendCallback@e46a817] in state PROCESSING
at org.eclipse.jetty.util.IteratingCallback.close(IteratingCallback.java:428)
at org.eclipse.jetty.server.HttpConnection.onClose(HttpConnection.java:524)
at org.eclipse.jetty.io.ssl.SslConnection.onClose(SslConnection.java:378)
at org.eclipse.jetty.io.SelectorManager.connectionClosed(SelectorManager.java:345)
at org.eclipse.jetty.io.ManagedSelector$DestroyEndPoint.run(ManagedSelector.java:1104)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883)
at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034)
at java.base/java.lang.Thread.run(Unknown Source)
我可以用
@ExceptionHandler
轻松捕捉到这一点。但是,由于通道已经关闭,我的 ExceptionHandler 的响应会导致后续异常:
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver: Failure in @ExceptionHandler com.acme.controller.CommonControllerErrorHandler#handleUncaught(Exception)
org.eclipse.jetty.io.EofException: Closed
at org.eclipse.jetty.server.HttpOutput.checkWritable(HttpOutput.java:771)
at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:795)
at org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:638)
at org.springframework.util.StreamUtils$NonClosingOutputStream.write(StreamUtils.java:287)
at com.fasterxml.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:2203)
at com.fasterxml.jackson.core.json.UTF8JsonGenerator.flush(UTF8JsonGenerator.java:1197)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1063)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:456)
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:104)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:219)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:78)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:135)
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:428)
我尝试根本不发送回复,但这些方法失败了:
ExceptionHandler
的 ResponseEntity
并返回 null
ExceptionHandler
,其返回类型为 void
,参数类型为 HttpServletResponse
,并且
reset()
相反,Spring MVC 默认会返回
HTTP-200 OK
和 Cache-Control
标头。@Peter,你找到解决办法了吗?我们遇到了完全相同的问题,当 ExceptionHandler 尝试写入其响应时,IOException 和 Jetty 的 EofException 分别触发另一个 EofException。