title: Spring Boot - Multipart file maximum upload size exception
body: <p>I'm developing a endpoint with Spring Boot <strong>2.1.3</strong> (using the standard Tomcat embedded web server) to upload an image and I want to limit the size of the multipart upload. I can do this easily with the properties:</p>
  
  <pre><code>spring:
      servlet:
          multipart:
              max-file-size: 2MB
              max-request-size: 2MB
  </code></pre>
  
  <p>But I always get a 500 that Spring can't catch because is Tomcat that is throwing the exception and the request don't reach my code in the RestController.</p>
  
  <pre><code>2019-03-02 10:12:50.544 ERROR [] [   o.a.c.c.C.[.[.[/].[dispatcherServlet]] [] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3917946) exceeds the configured maximum (2097152)] with root cause 
  org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3917946) exceeds the configured maximum (2097152)
  </code></pre>
  
  <p>My ExceptionHandler is like this but obviously don't work whatever the exception that put in the annotation:</p>
  
  <pre><code>@ExceptionHandler(MaxUploadSizeExceededException.class)
  public ResponseEntity handleMaxSizeException(Exception e) {
      logger.warn(e.getMessage());
      ...
      return status(HttpStatus.PAYLOAD_TOO_LARGE).body(...);
  }
  </code></pre>
  
  <p>I already tried this with the already mentioned properties but without any effect:</p>
  
  <pre><code>@Bean
  public TomcatServletWebServerFactory containerFactory() {
      TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
      factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -&gt; 
          ((AbstractHttp11Protocol&lt;?&gt;) connector.getProtocolHandler()).setMaxSwallowSize(-1));
      return factory;
  }
  </code></pre>
  
  <p>I already checked the answers in the following questions (and others...) but they are mostly outdated or simply don't work:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/35748022/multipart-file-maximum-size-exception-spring-boot-embbeded-tomcat">Multipart file maximum size exception - spring boot embbeded tomcat</a></li>
  <li><a href="https://stackoverflow.com/questions/14298265/maxuploadsizeexceededexception-doesnt-invoke-the-exception-handling-method-in-s">MaxUploadSizeExceededException doesn&#39;t invoke the exception handling method in Spring</a>   </li>
  <li><a href="https://stackoverflow.com/questions/2689989/how-to-handle-maxuploadsizeexceededexception">How to handle MaxUploadSizeExceededException</a></li>
  </ul>
  
  <p>Is somebody struggling with this?</p>