title: Use RestTemplate with object as data and application/x-www-form-urlencoded content type?
body: <p>I need to post an object (e.g. not a <code>MultiValueMap</code>) via a <code>RestTemplate</code> with the content type <code>application/x-www-form-urlencoded</code>. When I try to do so ...</p>
  
  <pre class="lang-java prettyprint-override"><code>HttpHeaders headers = new HttpHeaders();
  HttpEntity request;
  
  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED)
  
  // data is some generic type
  request = new HttpEntity&lt;&gt;(data, headers);
  
  // clazz is the Class&lt;T&gt; being returned
  restTemplate.exchange(url, method, request, clazz)
  </code></pre>
  
  <p>... I get the following error:</p>
  
  <blockquote>
    <p>org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.whatever.MyRequestPayload] and content type [application/x-www-form-urlencoded]</p>
  </blockquote>
  
  <p>Here is what I see within <code>restTemplate.getMessageConverters()</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/SloDy.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SloDy.png" alt="message converters"></a></p>
  
  <p><em>Why don't I want to provide a <code>MultiValueMap</code>?</em> Two reasons:</p>
  
  <ol>
  <li>this is general purpose code which is used to send requests to multiple endpoints, so adding an overload specifically for <code>x-www-form-urlencoded</code> will only complicate things</li>
  <li>it doesn't seem like I should have to -- I just don't know which HttpMessageConverter needs to be used to support converting objects to a <code>x-www-form-urlencoded</code> string</li>
  </ol>