我正在使用ws
查询spotify api以查找给定查询的曲目列表,当我将JSON数据转换为案例类时我得到的错误是我'还没弄明白...
class SearchController @Inject() (
val ws: WSClient
) extends Controller {
case class TrackSearch(href: String)
implicit val trackResultsReads: Reads[TrackSearch] = (
(__ \ "tracks" \ "href").read[String]
)(TrackSearch.apply _)
def index = Action.async { implicit request =>
search("track", param(request, "q")).map { r =>
val ts = r.json.as[TrackSearch]
println(ts)
Ok
}
}
private def search(category: String, query: String): Future[Try[WSResponse]] = {
ws.url("https://api.spotify.com/v1/search")
.withQueryString("q" -> query, "type" -> category)
.get()
.map(Success(_))
.recover { case x => Failure(x) }
}
private def param(request: Request[AnyContent], name: String): String = {
request.queryString.get(name).flatMap(_.headOption).getOrElse("")
}
}
我得到的错误是:
Overloaded method value [read] cannot be applied to (String => SearchController.this.TrackSearch)
implicit val trackResultsReads: Reads[TrackSearch]
> (__ \ "tracks" \ "href").read[String]
)(TrackSearch.apply _)
如果我在动作中查询JSPath,我可以得到" href"字符串返回正常,所以不是这样:
println(r._2.json \ "tracks" \ "href")