加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 在Play框架Web服务参数中有一个List

发布时间:2020-12-16 09:26:33 所属栏目:安全 来源:网络整理
导读:我在play框架中编写了这个web服务. 调节器 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
我在play框架中编写了这个web服务.

调节器

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)

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

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

解决方法

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

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签名更改为:

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

最后的网址将是@mfirry显示的内容:

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读