title: Spring POST multipart/form-data, request parts always empty
body: <p>I have a simple REST controller that I use for accepting a file being uploaded from a HTML form. The project is Spring Boot 2.6.1 and Java 17. But the problem was also to be found in Spring Boot 2.3.7 and Java 15.</p>
  <pre><code>@PostMapping(path = &quot;/file&quot;, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public void handleFileUpload(@RequestParam(&quot;file&quot;) MultipartFile file) {
      fileService.upload(file.getInputStream(), file.getOriginalFilename());
  }
  </code></pre>
  <p>The problem is <code>file</code> is always NULL. I found a lot of different answers about setting a <code>MultipartResolver</code> bean or enabling <code>spring.http.multipart.enabled = true</code> but nothing helped. I have a logging filter as one of the first filters in the chain. After debugging in the filter chain I found out that making a call to <code>request.getParts()</code> made everything work. My filter look like this:</p>
  <pre><code>public class LoggingFilter extends GenericFilterBean {
  
      @Override
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
          HttpServletRequest httpServletRequest = (HttpServletRequest) request;
          BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(httpServletRequest);
          BufferedResponseWrapper bufferedResponse = new BufferedResponseWrapper((HttpServletResponse) response);
  
          filterChain.doFilter(bufferedRequest, bufferedResponse);
  
          logRequest(httpServletRequest, bufferedRequest);
          logResponse(httpServletRequest, bufferedResponse);
      }
  </code></pre>
  <p>I changed the filter to:</p>
  <pre><code>public class LoggingFilter extends GenericFilterBean {
  
      @Override
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
          HttpServletRequest httpServletRequest = (HttpServletRequest) request;
  
          if (request.getContentType() != null &amp;&amp; request.getContentType().startsWith(&quot;multipart/form-data&quot;)) {
              httpServletRequest.getParts(); // Trigger initialization of multi-part.
          }
  
          BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(httpServletRequest);
          BufferedResponseWrapper bufferedResponse = new BufferedResponseWrapper((HttpServletResponse) response);
  
          filterChain.doFilter(bufferedRequest, bufferedResponse);
  
          logRequest(httpServletRequest, bufferedRequest);
          logResponse(httpServletRequest, bufferedResponse);
      }
  </code></pre>
  <p>and everything was working. My question is; why is this needed? And is there a better way of doing this?</p>