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

scala – 如何将RequestReader绑定到Finch中的Route

发布时间:2020-12-16 09:55:18 所属栏目:安全 来源:网络整理
导读:我想知道如何在Finch中将RequestReader和Route绑定在一起.我没有找到一个完整的例子. 这个例子来自finch github,它运行正常. import io.finch.route._import com.twitter.finagle.Httpxval api: Router[String] = get("hello") { "Hello,World!" }Httpx.serv
我想知道如何在Finch中将RequestReader和Route绑定在一起.我没有找到一个完整的例子.

这个例子来自finch github,它运行正常.

import io.finch.route._
import com.twitter.finagle.Httpx

val api: Router[String] = get("hello") { "Hello,World!" }

Httpx.serve(":3000",api.toService)

我明白这段代码将获得路径“hello”并将返回响应“hello world”

然后我想将RequestHeader绑定到它.

val doSomethingWithRequest: RequestReader[String] =
    for {
      foo <- param("foo")
      bar <- param("bar")
    } yield "u got me"

  val api: Router[RequestReader[String]] = Get / "hello" /> doSomethingWithRequest

  val server = Httpx.serve(":3000",api.toService)

我认为这段代码意味着如果网址被赋予“http://localhost:3000/hello?foo=3”,它将返回“你找我”的回复.但是,响应状态为404.

我认为我对Route和RequestHeader之间的组合做错了.

也许有人可以帮助我这个,也就是说,分享一些关于这个芬奇的好文章会更好.该版本频繁出现,文档已过时https://finagle.github.io/blog/2014/12/10/rest-apis-with-finch/

解决方法

谢谢你问这个!我相信这是StackOverflow上第一个有关Finch的问题.

从0.8(即has been released today)开始,使用?组合路由器和RequestReader是完全可能的.组合器(更多详细信息,请参见第“Composing Routers”节).

以下是说明此功能的示例.

// GET /hello/:name?title=Mr.
val api: Router[String] = 
  get("hello" / string ? param("title")) { (name: String,title: String) =>
    s"Hello,$title$name!"
  }
Httpx.serve(":8081",api.toService)

你提到的博客文章已经过时了,所有博客帖子都是如此.虽然,在Github回购中有一个comprehensive documentation,我们正试图保持实际.

(编辑:李大同)

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

    推荐文章
      热点阅读