[com.example.blog.SnapEngChatRequest]和内容类型[application/x-www-form-urlencoded]没有HttpMessageConverter [英] No HttpMessageConverter for [com.example.blog.SnapEngChatRequest] and content type [application/x-www-form-urlencoded]

查看:405
本文介绍了[com.example.blog.SnapEngChatRequest]和内容类型[application/x-www-form-urlencoded]没有HttpMessageConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用表单URL编码的标头调用post API.这是我的代码

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]

在这里,我正在提供httpMessageConverter,但是我不确定为什么它没有占用,或者我不确定在这里是否做错了什么.我已经尝试了所有可能的方法.任何帮助都会有所帮助,谢谢!

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!

推荐答案

来自

...读取和写入"application/x-www-form-urlencoded"媒体类型 作为MultiValueMap

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

因此它无法从POJO中读取它.像这样发送您的数据:

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"))
)

这篇关于[com.example.blog.SnapEngChatRequest]和内容类型[application/x-www-form-urlencoded]没有HttpMessageConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆