我想调用带有表单URL编码标头的post API。这是我的代码
var data = SnapEngChatRequest(
widgetId = widgetId,
visitorMessage = "Test"
)
val headers = HttpHeaders()
headers.set("x-api-key", apiKey)
headers.set("Content-Type", "application/x-www-form-urlencoded")
val entity = HttpEntity(data, headers)
val converter = FormHttpMessageConverter()
converter.supportedMediaTypes = singletonList(MediaType.APPLICATION_FORM_URLENCODED)
restTemplate.messageConverters.add(converter)
val result = restTemplate.exchange(
url,
HttpMethod.POST,
entity,
String::class.java
)
但遗憾的是,它无法正常工作,而且我的误差低于此值
No HttpMessageConverter for [com.example.blog.SnapEngChatRequest] and content type [application/x-www-form-urlencoded]
org.springframework.web.client.RestClientException: No HttpMessageConverter for [com.example.blog.SnapEngChatRequest] and content type [application/x-www-form-urlencoded]
在这里,我给了httpMessageConverter,但我不确定它为什么不采取或我不确定我在这里做错了什么。我尽力了。任何帮助都会有所帮助,谢谢!
从FormHttpMessageConverter的文档中可以:
...读取和写入“application / x-www-form-urlencoded”媒体类型为MultiValueMap
所以它无法从POJO中读取它。像这样发送您的数据:
val data = LinkedMultiValueMap(
mapOf("widgetId" to listOf(widgetId), "visitorMessage" to listOf("Test"))
)