【发布时间】:2019-09-12 17:15:35
【问题描述】:
我想调用带有表单 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,但我不确定它为什么没有使用,或者我不确定我是否在这里做错了什么。我已经尝试了所有可能的方法。任何帮助都会有所帮助,谢谢!
【问题讨论】:
标签: spring-boot kotlin resttemplate