Note that there are some explanatory texts on larger screens.

plurals
  1. POMaxUploadSizeExceededException doesn't invoke the exception handling method in Spring
    text
    copied!<p>I'm using Spring 3.2.0. According to <a href="https://stackoverflow.com/a/3230352/1391249">this</a> answer, I have the same method in my annotated controller which implements the <code>HandlerExceptionResolver</code> interface such as,</p> <pre><code>public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;(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"); } </code></pre> <p>and the Spring configuration includes,</p> <pre><code>&lt;bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt; &lt;property name="maxUploadSize"&gt; &lt;value&gt;10000&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>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 <code>resolveException()</code> is never called even though the exception occurs. What is the way to handle this exception? Am I missing something?</p> <p>The same thing is also specified <a href="http://www.raistudies.com/spring/spring-mvc/file-upload-spring-mvc-annotation/" rel="nofollow noreferrer">here</a>. I'm not sure why it doesn't work in my case.</p> <hr> <p>I have tried the <a href="http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/#controlleradvice" rel="nofollow noreferrer">following approach</a> with <a href="http://static.springsource.org/spring-framework/docs/3.2.0.M2/api/org/springframework/web/bind/annotation/ControllerAdvice.html" rel="nofollow noreferrer"><code>@ControllerAdvice</code></a> but it didn't work either.</p> <pre><code>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&lt;Object&gt; handleConflict(RuntimeException ex, WebRequest request) { String bodyOfResponse = "This should be application specific"; return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request); } } </code></pre> <p>I have also tried to put the verbose - <code>Exception</code>.</p> <pre><code>@ExceptionHandler(value={Exception.class}) </code></pre> <p>The method <code>ResponseEntity()</code> is never invoked in any case.</p> <hr> <p>In general, I want to handle this exception per controller base (controller level), if possible. For this, one <code>@ExceptionHandler</code> 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 <code>web.xml</code> file. If this is not even feasible, then this exception should be handled anyway without any custom requirements I have just expressed.</p> <p>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?</p> <hr> <p>What I'm getting after the exception is thrown can be visible in the following <a href="https://i.stack.imgur.com/GrKgq.png" rel="nofollow noreferrer">snap shot</a>.</p> <p><img src="https://i.stack.imgur.com/GrKgq.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload