【发布时间】:2016-03-22 12:57:11
【问题描述】:
我创建了以下 Kotlin 数据类:
@JsonInclude(JsonInclude.Include.NON_NULL)
public data class ITunesArtist(val artistName: String,
val artistId: Long, val artistLinkUrl: URL)
(数据类是 Kotlin 类,它在编译时自动生成 equals、hashcode、toString 等 - 节省时间)。
现在我尝试使用 Spring RestTemplate 填充它:
@Test
fun loadArtist()
{
val restTemplate = RestTemplate()
val artist = restTemplate.getForObject(
"https://itunes.apple.com/search?term=howlin+wolf&entity=allArtist&limit=1", ITunesQueryResults::class.java);
println("Got artist: $artist")
}
它失败了:
Could not extract response: no suitable HttpMessageConverter found for response type
[class vampr.api.service.authorization.facebook.ITunesArtist]
and content type [text/javascript;charset=utf-8]
很公平 - JSON 对象映射器可能期望 mime 类型为 text/json。除了告诉RestTemplate 映射到String::class.java,然后手动实例化JacksonObjectMapper 的实例之外,有没有办法告诉我的RestTemplate 将返回的mime 类型视为JSON?
【问题讨论】:
标签: java spring spring-boot kotlin spring-rest