次の Kotlin データ クラスを作成しました。
@JsonInclude(JsonInclude.Include.NON_NULL)
public data class ITunesArtist(val artistName: String,
val artistId: Long, val artistLinkUrl: URL)
(データ クラスは、コンパイル時に equals、hashcode、toString などを自動生成する Kotlin クラスであり、時間を節約します)。
今、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-type を期待していtext/json
ました。RestTemplate
にマップするように指示してからString::class.java
のインスタンスをJacksonObjectMapper
手動でインスタンス化する以外にRestTemplate
、返された MIME タイプを JSON として扱うように指示する方法はありますか?