一尘不染

请求上传期间如何处理客户端中止?

spring-boot

给定以下应用程序,基于Spring Initializr模板

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    public static class Payload {
        public String field1;
        public String field2;
    }

    @RestController
    public static class MyController {
        @RequestMapping("/echo")
        public ResponseEntity<Payload> echo(@RequestBody Payload payload) {
            return new ResponseEntity<>(payload, HttpStatus.OK);
        }
    }
}

如果在发送标头后中止连接

$ nc 192.168.56.1 8080
POST /echo HTTP/1.1
Host: 192.168.56.1:8080
Content-Type: application/json
Content-Length: 42

^C

然后服务器尝试用400响应,并记录以下内容

2016-05-03 16:56:28.916  WARN 5776 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unexpected EOF read on the socket; nested exception is java.io.EOFException: Unexpected EOF read on the socket
2016-05-03 16:56:28.997 ERROR 5776 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost]           : Exception Processing ErrorPage[errorCode=0, location=/error] org.apache.catalina.connector.ClientAbortException: java.io.IOException: An established connection was aborted by the software in your host machine

相反,如果客户端在请求发送后中止,则默认情况下不记录任何内容,结果ClientAbortException可通过过滤器使用ErrorAttributes#getError

如何以相同方式处理请求期间中止?也就是说,默认情况下不会记录任何警告/错误,并启用自定义过滤器来告知客户端已中止。


阅读 476

收藏
2020-05-30

共1个答案

一尘不染

我已经找到了解决方案,但还没有找到解决问题的案例。

@Autowired
private DefaultErrorAttributes defaultExceptionResolver;

@ExceptionHandler(HttpMessageNotReadableException.class)
public void checkForAbort(HttpServletRequest request, HttpServletResponse response, RuntimeException exception) {
    if (exception.getCause() instanceof EOFException) {
        ClientAbortException abortException = new ClientAbortException(exception.getCause());
        abortException.addSuppressed(exception);

        try {
            response.getOutputStream().close();
        }
        catch (IOException ex) {
            abortException.addSuppressed(ex);
        }

        defaultExceptionResolver.resolveException(request, response, null, abortException);
    }
    else {
        throw exception;
    }
}

例如,这可能会进入@ControllerAdvice

2020-05-30