在 Play 框架 Web 服务参数中有一个列表 [英] Have a List in Play framework web service parameters

查看:18
本文介绍了在 Play 框架 Web 服务参数中有一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 play 框架中编写了这个网络服务.

I have written this web service in play framework.

控制器

  def getByGenre(genre: String) = Action {
    val result = Await.result(Movies.getByGenre(genre), 5 seconds)
    Ok(toJson(result))
  }

路线

GET     /movies/genre/:genre              controllers.MoviesController.getByGenre(genre: String)

但是,用户可以选择多个流派.因此我需要将流派参数转换为 List[String]

However a user may select multiple Genre. Therefore I need to convert the genre parameter to a List[String]

我还需要知道如何使用 CURL 将该 Array 参数传递给 Web 服务.

I also need to know how to pass that Array parameter to the web service using CURL.

推荐答案

如果您可以将 genres 参数作为查询字符串的一部分传递,只需用不同的值重复该参数,然后像这样检索它这个:

If you can pass the genres parameter as part of the query string, just repeat the parameter with different values and then retrieve it like this:

def getByGenre() = Action.async { implicit request =>
    val genres = request.queryString.get("genres")
    Movies.getByGenre(genres).map { movies =>
      Ok(toJson(movies))
    }
}

您的路线将是:

GET    /movies/genre          controllers.MoviesController.getByGenre()

另外,请注意您需要将 Movies.getByGenre 签名更改为:

Also, notice that you will need to change the Movies.getByGenre signature to:

def getByGenre(genres: Option[Seq[String]]): Seq[Movies]

最终 url 将类似于@mfirry 显示的内容:

An final url will be something like @mfirry showed:

myhost.com/movies/genre?genre=action&genre=drama

最后,您可能已经注意到,我已从您的操作中删除了阻止代码.在控制器上使用 Await 意味着在最坏的情况下,您的操作将被阻塞至少 5 秒.我建议您查看以下 Play 文档页面:

Finally, as you may have noticed, I've removed the blocking code from you action. Using Await at your controller means that you action would be blocking for at least 5 seconds at the worst case scenario. I suggest you to take a look at the following page of Play docs:

https://www.playframework.com/documentation/2.5.x/ScalaAsync

这篇关于在 Play 框架 Web 服务参数中有一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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