我正在使用Spring 3.2.0.根据这个答案,我在带注释的控制器中使用相同的方法来实现HandlerExceptionResolver
接口,例如:
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");
}
Run Code Online (Sandbox Code Playgroud)
和Spring配置包括,
<bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>10000</value>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
当文件大小超过时,应该调用前面的方法,它应该自动处理异常,但根本不会发生.resolveException()
即使发生异常,也不会调用该方法.处理此异常的方法是什么?我错过了什么吗?
这里也指出了同样的事情.我不确定为什么它在我的情况下不起作用.
我尝试了以下方法,@ControllerAdvice
但它也没有用.
package exceptionhandler;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import …
Run Code Online (Sandbox Code Playgroud) spring exception-handling file-upload spring-mvc handlerexceptionresolver
我对此进行了一些研究,结果相互矛盾.为了处理这个错误,有人说我需要HandlerExceptionResolver
在我的一个控制器中实现.
以下是一些链接:
另一方面,其他人说这种方法是徒劳的,因此Exception
在请求处理流程之外发生:
我尝试了上述解决方案,但它们对我不起作用.似乎Exception
发生在Spring之外,正如预期的那样.我甚至无法抓住这个HandlerExceptionResolver
.