【发布时间】:2019-06-12 14:13:18
【问题描述】:
我需要通过内容类型为application/x-www-form-urlencoded 的RestTemplate 发布一个对象(例如,不是MultiValueMap)。当我尝试这样做时...
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)
...我收到以下错误:
org.springframework.web.client.RestClientException: 无法写入请求:没有为请求类型 [com.whatever.MyRequestPayload] 和内容类型 [application/x-www-form-urlencoded] 找到合适的 HttpMessageConverter
这是我在restTemplate.getMessageConverters() 中看到的:
我为什么不想提供MultiValueMap?两个原因:
- 这是用于向多个端点发送请求的通用代码,因此专门为
x-www-form-urlencoded添加重载只会使事情复杂化 - 似乎我不应该这样做——我只是不知道需要使用哪个 HttpMessageConverter 来支持将对象转换为
x-www-form-urlencoded字符串
【问题讨论】:
标签: java spring spring-boot