gpt4 book ai didi

spring - MaxUploadSizeExceededException 不调用 Spring 中的异常处理方法

转载 作者:IT老高 更新时间:2023-10-28 13:46:15 25 4
gpt4 key购买 nike

我使用的是 Spring 3.2.0。根据this答案,我在带注释的 Controller 中有相同的方法,它实现了 HandlerExceptionResolver 接口(interface),例如,

public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {

Map<String, Object> model = new HashMap<String, Object>(0);

if (exception instanceof MaxUploadSizeExceededException) {
model.put("msg", exception.toString());
model.put("status", "-1");
} else {
model.put("msg", "Unexpected error : " + exception.toString());
model.put("status", "-1");
}

return new ModelAndView("admin_side/ProductImage");
}

Spring 配置包括,

<bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>10000</value>
</property>
</bean>

当文件大小超过时,应该调用前面的方法,它应该自动处理异常,但它根本不会发生。即使发生异常,也永远不会调用 resolveException() 方法。处理这个异常的方法是什么?我错过了什么吗?

同样的事情也被指定here .我不确定为什么它在我的情况下不起作用。


我试过 following approach@ControllerAdvice但它也没有工作。

package exceptionhandler;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public final class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(value = {MaxUploadSizeExceededException.class})
protected ResponseEntity<Object> handleConflict(RuntimeException ex, WebRequest request) {
String bodyOfResponse = "This should be application specific";
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
}
}

我也试过把冗长的 - Exception.

@ExceptionHandler(value={Exception.class})

方法 ResponseEntity() 在任何情况下都不会被调用。


一般来说,如果可能,我想按 Controller 基础( Controller 级别)处理此异常。为此,一个 @ExceptionHandler 注释方法应该只对那个特定的 Controller 有效,而不是对整个应用程序全局有效,因为在我的应用程序中只有几个网页处理文件上传。当这个异常发生时,我只想在当前页面显示一个用户友好的错误信息,而不是重定向到 web.xml 文件中配置的错误页面。如果这甚至不可行,那么无论如何都应该处理这个异常,而不需要我刚才表达的任何自定义要求。

这两种方法都不适合我。我找不到更多关于处理这个异常的信息。它是否需要在 XML 文件中的某处或其他地方进行额外配置?


抛出异常后我得到的结果可以在以下 snap shot 中看到.

enter image description here

最佳答案

根据您发布的堆栈跟踪,MaxUploadSizeExceeded 异常会在请求到达调度程序 servlet 之前引发。因此不会调用您的异常处理程序,因为在抛出异常时目标 Controller 尚未确定。

如果您查看堆栈跟踪,您会发现异常是在 HiddenHttpMethodFilter 中引发的,该异常会获取您的多部分请求的所有参数 - 以及您的“to big”上传数据参数。

您的 Controller 是否需要 HiddenHttpMethodFilter 来处理多部分上传?如果没有,请将此过滤器从您的上传处理 Controller 中排除。

关于spring - MaxUploadSizeExceededException 不调用 Spring 中的异常处理方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14298265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com