Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaxUploadSizeExceededException doesn't invoke the exception handling method in Spring

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.

enter image description here

like image 492
Tiny Avatar asked Jan 12 '13 21:01

Tiny


People also ask

How do you handle MaxUploadSizeExceededException?

And to handle the MaxUploadSizeExceededException you can add ControllerAdvice with ExceptionHandler. otherwise that exception will be triggered before the request is mapped to controller.

Does spring provide exception handling?

Spring MVC provides exception handling for your web application to make sure you are sending your own exception page instead of the server-generated exception to the user. The @ExceptionHandler annotation is used to detect certain runtime exceptions and send responses according to the exception.

How do I handle exceptions in spring boot?

Exception HandlerThe @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.

What is spring ControllerAdvice?

@ControllerAdvice is a specialization of the @Component annotation which allows to handle exceptions across the whole application in one global handling component. It can be viewed as an interceptor of exceptions thrown by methods annotated with @RequestMapping and similar.


1 Answers

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.

like image 119
Dirk Lachowski Avatar answered Oct 12 '22 02:10

Dirk Lachowski