title: How to handle org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException
body: <p>I develop spring boot application used as middleware between client apps and backend application. I try to upgrade spring boot version from 1.5.6.RELEASE to 2.1.12.RELEASE. </p>
  
  <p>After upgrade, there are some exceptions that I can handle later. However, I can't fix the problem related with <strong>content type</strong> in integration tests that I develop with spring-boot-test.</p>
  
  <p>Here is the problematic service signature:</p>
  
  <pre><code>@RequestMapping(
      value = {"test/lorem/ipsum/{id}"},
      produces = {"application/json"},
      consumes = {"application/x-www-form-urlencoded"},
      method = {RequestMethod.POST}
  )
  RedirectView testMethod(@RequestPart(value = "redirectResponse",required = true) String var1, @PathVariable("id") String var2);
  </code></pre>
  
  <p>Here is the test method:</p>
  
  <pre><code>@Test
  public void testSuccessResponse() {
      final MultiValueMap&lt;String, String&gt; map = new LinkedMultiValueMap&lt;&gt;();
      map.add(MockConstant.REDIRECT_RESPONSE, MockConstant.REDIRECT_SUCCESS_RESPONSE);
  
      final String url = "test/lorem/ipsum/" + MockConstant.ID;
      final ResponseEntity&lt;RedirectView&gt; responseEntity = post(
          url,
          map,
          MediaType.APPLICATION_FORM_URLENCODED,
          RedirectView.class);
  
      ...
  }
  </code></pre>
  
  <p>Related post method:</p>
  
  <pre><code>protected final &lt;T&gt; ResponseEntity&lt;T&gt; post(final String path, final Object request, final MediaType mediaType,
      final Class&lt;T&gt; responseType) {
  
      final HttpEntity&lt;?&gt; requestEntity = new HttpEntity&lt;&gt;(request, getHttpHeaders(mediaType));
  
      return restTemplate.exchange(path, HttpMethod.POST, requestEntity, responseType);
  }
  </code></pre>
  
  <p>Related getHttpHeaders method:</p>
  
  <pre><code>protected HttpHeaders getHttpHeaders(final MediaType mediaType) {
  
      final HttpHeaders headers = new HttpHeaders();
  
      headers.setContentType(mediaType);
      headers.set(HttpHeaders.ACCEPT_LANGUAGE, "tr");
      headers.set(Constant.HEADER_PLATFORM, "ios");
      headers.set(Constant.HEADER_APP_VERSION, "3.4.0");
      headers.set(Constant.HEADER_SYSTEM_VERSION, "20.0");
  
      return headers;
  }
  </code></pre>
  
  <p>While the test is running, it cannot get into the service and thrown this exception below:</p>
  
  <pre><code>    org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded;charset=UTF-8
          at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure(StandardMultipartHttpServletRequest.java:124)
          at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:115)
          at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.&lt;init&gt;(StandardMultipartHttpServletRequest.java:88)
          at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.&lt;init&gt;(StandardMultipartHttpServletRequest.java:72)
          at org.springframework.web.multipart.support.MultipartResolutionDelegate.asMultipartHttpServletRequest(MultipartResolutionDelegate.java:78)
          at org.springframework.web.multipart.support.RequestPartServletServerHttpRequest.&lt;init&gt;(RequestPartServletServerHttpRequest.java:66)
          at org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:134)
          at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:127)
          at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
          at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
          at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
          at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)
          at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798)
          at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
          at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
          at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
          at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
          at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
          at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
          at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
          at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:88)
          at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
          at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
          at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
          at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
          at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
          at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
          at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
          at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:151)
          at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:86)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
          at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:114)
          at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:104)
          at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
          at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
          at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
          at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
          at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
          at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
          at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
          at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)
          at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
          at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
          at java.lang.Thread.run(Thread.java:748)
  Caused by: javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded;charset=UTF-8
          at org.apache.catalina.connector.Request.parseParts(Request.java:2907)
          at org.apache.catalina.connector.Request.getParts(Request.java:2769)
          at org.apache.catalina.connector.RequestFacade.getParts(RequestFacade.java:1098)
          at javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:359)
          at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:95)
          ... 69 common frames omitted
  Caused by: org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded;charset=UTF-8
          at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:140)
          at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.getMultiPartStream(FileItemIteratorImpl.java:194)
          at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.findNextItem(FileItemIteratorImpl.java:213)
          at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.&lt;init&gt;(FileItemIteratorImpl.java:131)
          at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:255)
          at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:279)
          at org.apache.catalina.connector.Request.parseParts(Request.java:2867)
          ... 73 common frames omitted
  </code></pre>
  
  <p>I don't know what I can share any other info about the problem. If you think there should, please ask me to share with you.</p>
  
  <p>Any ideas would be appreciated,
  Best regards.</p>