Announcement Announcement Module
No announcement yet.
Handling MaxUploadSizeExceededException in Spring Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Handling MaxUploadSizeExceededException in Spring

    I'm using Spring 3.2.0. I have implemented the resolveException() method in my annotated controller which implements the HandlerExceptionResolver interface such as,

    public final class ProductImageController implements 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");
                   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">
    as specified here.

    When the file size exceeds, the method in the controller class should be invoked and it should automatically handle the exception but it doesn't happen at all. What I'm getting is the full stack trace on the browser. The method resolveException() is never called even though the exception occurs. What is the way to handle this exception? Am I missing something?

    I have also tried registering a separate bean annotated with @Component in application-context.xml but nothing worked.
    Last edited by Tiny; Jan 13th, 2013, 03:08 PM.

  • #2
    The problem is the exception occurs outside of the flow which is invoked for a controller so in this case the HandlerExceptionResolver isn't going to do much (the name indicates this HandlerExceptionResolver handles exceptions occuring in the handler not before or after the handler execution).

    Also not sure why you have a @Controller implementing that interface (I strongly suggest a read of the reference guide).

    You can defer the multipart parsing (set resolveLazily to true on the CommonsMultipartResolver) this might make the exception occur during injection of the parameters on the method which might make it part of the request handling flow. However that depends on your configuration and which HandlerAdapter you use).

    2 other ways are rely on the exception handling of the servlet container or in other words specify error-pages in the servlet container or extend the DispatcherServlet.
    Last edited by Marten Deinum; Jan 13th, 2013, 10:08 AM.


    • #3
      Setting resolveLazily to true unfortunately didn't work. I'm yet not able to display the form when this exception occurs and a user-friendly error message by handling this exception (which should be on the same form). I'm getting the full stack trace on the page.

      <bean id="filterMultipartResolver"
      Last edited by Tiny; Jan 13th, 2013, 12:32 PM.