简体   繁体   English

喷雾路径适用于单斜线,但没有其他功能

[英]Spray routing works for single slash but nothing else

So i have asked about this before and have changed a lot of code around. 所以我之前曾问过这个问题,并更改了很多代码。

Spray Routing Doesn't match anything 喷涂路线不匹配

Now I am executing my functions that return HTTPresponses insided detach() blocks so that i dont block. 现在,我正在执行返回在detach()块内返回​​HTTP响应的函数,以使我不会阻塞。 These then are completed and return to the client, but I still can't seem to get my routing to work. 然后这些就完成了并返回给客户端,但是我似乎仍然无法使我的路由正常工作。

In my tests, a request to a single slash works fine, but anything else, such as this create user path shown below fails. 在我的测试中,对单个斜杠的请求可以正常工作,但其他任何事情(例如下面显示的创建用户路径)都会失败。 I can't seem to figure out why, and spray routing uses so many constructs I'm having a hard time figuring out how the system works well enough to find out whats happening. 我似乎无法弄清原因,喷涂工艺使用了如此多的构造,我很难弄清系统如何运作得足够好以找出正在发生的事情。

I tried inserting logRequest blocks around certain paths thinking that might show me whats happening, but none of them seem to get hit. 我尝试在某些路径周围插入logRequest块,以为可能会告诉我发生了什么事,但是似乎都没有遇到任何问题。 Any help would be greatly appreciated. 任何帮助将不胜感激。

val route: Route = {
    host("fakebook.com", "www.fakebook.com") {
      pathSingleSlash {
        complete("pong")
      } ~
        pathPrefix("users") { req =>
            path("newuser") {
                put {
                  detach() {
                    complete(genericPut(CreateUser(req.request)))
                  }
                }
            } ~
            ... rest of routing

And here is what my scalatests look like, the simple Put passes, but the put with newuser doesn't 这是我的scalatests的样子,简单的Put通过了,但是newuser的put没有

val createUserSuccessRequest = Put(Uri("http://www.fakebook.com/users/newuser") withQuery(F_User.lastNameString -> "Doe", F_User.firstNameString -> "John", F_User.bioString -> "i like foobar",
    F_User.ageString -> "42", F_User.dobString -> dateFormatter.format(new Date(1970 - 1900, 5, 7))))

 "The FakeBook route" when {

    "When sending a single slash request" should {
      "respond with a simple pong" in {
        Get() ~> logRequestResponse("plain get final request and response")(sealRoute(route)) ~> check {
          assert(responseAs[String] == "pong")
        }
      }
    }

    "Running route to create a user with PUT" should {
      "forward a message to the backbone to create a new user" in {
        createUserSuccessRequest ~> logRequest("create user final request and response"){sealRoute(route)} ~> check {
          expectMsg(CreateUser(createUserSuccessRequest))
        }
      }
    }
  }

For anyone else trying to solve this issue: 对于其他尝试解决此问题的人:

a lot of these directives actually DONT extract anything, so having the lambda inputs i have like req => and req2 => will not work. 这些指令很多实际上都不会提取任何东西,因此让我拥有的lambda输入就像req =>和req2 =>一样行不通。

It turns out, spray routing is designed so that you never have to touch the RequestContext as I have done with my functions (which is why I try to access it). 事实证明,喷雾路由的设计使您不必像我对函数所做的那样触摸RequestContext(这就是为什么我尝试访问它)。 They extract only the useful data. 他们仅提取有用的数据。 Rather than do things as I should and change my function signatures, i am going to (for now) do a hotfix that has worked. 与其做我应该做的事情并更改我的函数签名,不如(现在)做一个有效的修补程序。

if you absolutely must have the requestcontext, so long as you don't break it somehow, you can extract it by making your own extraction directive like so val extractRequestContext = extract(x => x) and wrap your code in that 如果您绝对必须具有requestcontext,只要您不以某种方式破坏它,就可以通过创建自己的提取指令来提取它,例如val extractRequestContext = extract(x => x)并将代码包装在其中

I did this 我做了这个

path("somepath") {
     detach() {
          extractRequestContext { request => complete(someFunc(request)) }
     }
}

In the future I should learn to use the DSL more correctly and extract what I need from the request context using directives and pass THOSE to the functions 将来,我应该学习更正确地使用DSL,并使用指令从请求上下文中提取所需的内容,并将THOSE传递给函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM