我正在尝试在Spring Boot中捕获MaxUploadSizeExceededException。

我尝试使用ControllerAdvice(具有看到的两个解决方案,有注释和无注释的一种:

@ControllerAdvice
public class FileUploadExceptionAdvice {

    Logger logger = LoggerFactory.getLogger(FileUploadExceptionAdvice.class);

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void springHandleNotFound(HttpServletResponse response) throws IOException {
        response.sendError(HttpStatus.NOT_FOUND.value());
    }

//    public ModelAndView handleMaxSizeException(MaxUploadSizeExceededException exc, HttpServletRequest request, HttpServletResponse response) {
//        ModelAndView mav = new ModelAndView();
//        mav.addObject("createOrUpdateError", "File too large!");
//        mav.setViewName("home");
//        return mav;
//    }

}

但是我看到的唯一变化是在日志中:

2020-07-29 10:02:57.504  WARN 1652 --- [nio-8081-exec-8] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (11532507) exceeds the configured maximum (10485760)]

重试几次后,用户将得到ERR_CONNECTION_RESET。

So I kept on digging and I found another answer on StackOverflow and implemented that solution too. This time nothing in my logs and the user got ERR_CONNECTION_RESET again.

我在做什么错,我该如何向模型发送错误消息并通知用户文件太大?