I need to post an object (e.g. not a MultiValueMap) via a RestTemplate with the content type application/x-www-form-urlencoded. When I try to do so ...
HttpHeaders headers = new HttpHeaders();
HttpEntity request;
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED)
// data is some generic type
request = new HttpEntity<>(data, headers);
// clazz is the Class<T> being returned
restTemplate.exchange(url, method, request, clazz)
... I get the following error:
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]
Here is what I see within restTemplate.getMessageConverters():
Why don't I want to provide a MultiValueMap? Two reasons:
- this is general purpose code which is used to send requests to multiple endpoints, so adding an overload specifically for
x-www-form-urlencodedwill only complicate things - 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
x-www-form-urlencodedstring
