Announcement Announcement Module
Collapse
No announcement yet.
Handling MaxUploadSizeExceededException in Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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,

    Code:
    @Controller
    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");
              }
              else
              {
                   model.put("msg", "Unexpected error : "+exception.toString());
                   model.put("status", "-1");
              }
              return new ModelAndView("admin_side/ProductImage");
         }
    }
    and the Spring configuration includes,

    Code:
    <bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize">
            <value>10000</value>
        </property>
    </bean>
    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.

    Comment


    • #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.

      Code:
      <bean id="filterMultipartResolver"
                p:resolveLazily="true"
                p:maxUploadSize="10000"
                class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
      Last edited by Tiny; Jan 13th, 2013, 12:32 PM.

      Comment

      Working...
      X