No HttpMessageConverter for [com.example.blog.SnapEngChatRequest] and content type [application/x-www-form-urlencoded]

一世执手 提交于 2019-12-24 07:15:57

问题


I wanted to call a post API with form URL encoded header. Here's my code

 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
    )

But unfortunately, it is not working and I'm getting below error

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]

Here, I'm giving the httpMessageConverter but I'm not sure why it is not taking or I'm not sure if I'm doing something wrong here. I have tried everything possible. Any help will be helpful, Thanks!


回答1:


From documentation for FormHttpMessageConverter it can:

... read and write the "application/x-www-form-urlencoded" media type as MultiValueMap

So it can't read it from a POJO. Send your data like this:

val data = LinkedMultiValueMap(
  mapOf("widgetId" to listOf(widgetId), "visitorMessage" to listOf("Test"))
)


来源:https://stackoverflow.com/questions/55812574/no-httpmessageconverter-for-com-example-blog-snapengchatrequest-and-content-ty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!