在Spring中,MaxUploadSizeExceptionException不会调用异常处理方法 [英] MaxUploadSizeExceededException doesn't invoke the exception handling method in Spring

查看:168
本文介绍了在Spring中,MaxUploadSizeExceptionException不会调用异常处理方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Spring 3.2.0。根据这个答案,我在我的注释控制器中有相同的方法,它实现了 HandlerExceptionResolver 界面,如

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

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

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

返回新的ModelAndView(admin_side / ProductImage);
}

,Spring配置包括

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

当文件大小超过时,应调用上述方法,并应自动处理异常根本没有发生。即使发生异常,也不会调用方法 resolveException()。处理这个异常的方法是什么?我是否缺少某些东西?



同样的事情也被指定为这里。我不知道为什么它在我的情况下不起作用。






我尝试过以下方法与< a href =http://static.springsource.org/spring-framework/docs/3.2.0.M2/api/org/springframework/web/bind/annotation/ControllerAdvice.html =nofollow noreferrer> @ControllerAdvice ,但它也无效。

 包装处理器

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 =这应该是应用程序特定的;
return handleExceptionInternal(ex,bodyOfResponse,new HttpHeaders(),HttpStatus.CONFLICT,request);
}
}

我也试图把详细 - 异常

  @ExceptionHandler(value = {Exception.class})

方法 ResponseEntity()从不在任何






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



这两种方法都不适合我。没有更多的关于处理这个异常我可以找到。它需要在XML文件中的其他配置吗?






在抛出异常后我得到的可以是在以下快照中可见。



解决方案

根据您发布的stacktrace,请求已经到达调度程序servlet之前,引发了 MaxUploadSizeExceeded 异常。因此,您的异常处理程序不会被调用,因为在抛出异常的情况下,目标控制器尚未确定。



如果您查看堆栈跟踪,可以看到 c $ c> HiddenHttpMethodFilter 中会抛出异常,它会获取您的multipart请求的所有参数,以及您的to bigupload-data参数。



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


I'm using Spring 3.2.0. According to this answer, I have the same method in my annotated controller which implements the HandlerExceptionResolver interface such as,

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");
}

and the Spring configuration includes,

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

When the file size exceeds, the preceding method should be invoked and it should automatically handle the exception but it doesn't happen at all. The method resolveException() is never called even though the exception occurs. What is the way to handle this exception? Am I missing something?

The same thing is also specified here. I'm not sure why it doesn't work in my case.


I have tried the following approach with @ControllerAdvice but it didn't work either.

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);
    }
}

I have also tried to put the verbose - Exception.

@ExceptionHandler(value={Exception.class})

The method ResponseEntity() is never invoked in any case.


In general, I want to handle this exception per controller base (controller level), if possible. For this, one @ExceptionHandler annotated method should only be active for that particular controller, not globally for the entire application, since there are only a few web pages in my application that handle file uploading. When this exception is caused, I just want to show a user-friendly error message on the current page and not to redirect to the error page configured in the web.xml file. If this is not even feasible, then this exception should be handled anyway without any custom requirements I have just expressed.

Neither of approaches worked for me. Nothing more about handling this exception I could find. Does it require additional configuration somewhere in XML files or otherwise?


What I'm getting after the exception is thrown can be visible in the following snap shot.

解决方案

According to your posted stacktrace the MaxUploadSizeExceeded exception is thrown before the request has reached the dispatcher servlet. Therefore your exceptionhandler isn't called because at the point the exception is thrown the target controller has yet to be determined.

If you look at the stacktrace you can see that the exception is thrown in the HiddenHttpMethodFilter that gets all parameters of your multipart-request - and also your "to big" upload-data parameter.

Is the HiddenHttpMethodFilter needed for your controller handling the multipart-upload? If not, exclude this filter from your upload-handling controllers.

这篇关于在Spring中,MaxUploadSizeExceptionException不会调用异常处理方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆