代码之家  ›  专栏  ›  技术社区  ›  Ali

如何在akkahttp中嵌套路由?

  •  0
  • Ali  · 技术社区  · 6 年前

    GET /

    POST / 应该打印“你好帖子”

    GET /foo

    POST /foo 应该打印“hello foo get”

    以下是我所拥有的:

    val route = pathSingleSlash {
        get(complete("hello get")) ~
        post(complete("hello post"))~
        path("foo") {
          get(complete("hello foo get"))~
          post(complete("hello foo post"))
        }
      }
    

    这适用于 岗位/ 但两者都得到和张贴 /foo

    我几乎什么都试过了,不知道该怎么办。说到这个,文档很难理解。

    有人能给我指点吗?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Raman Mishra    6 年前

    val route1 = path("foo") {
            get(complete("hello foo get")) ~
              post(complete("hello foo post"))
          }
    
      val route = pathSingleSlash {
        get(complete("hello get")) ~
          post(complete("hello post"))
      }
    
      val finalRoute = route ~ route1
    

    val bindingFuture = Http().bindAndHandle(finalRoute, "localhost", 8085)
    
        2
  •  1
  •   Mon Calamari    6 年前

    我建议以这种方式构建路径以获得最大的可读性:

    get & pathEndOrSingleSlash {
      complete("hello get")
    } ~
    post & pathEndOrSingleSlash {
      complete("hello post")
    } ~
    get & path("foo") & pathEndOrSingleSlash {
      complete("hello foo get")
    }
    post & path("foo") & pathEndOrSingleSlash {
      complete("hello foo post")
    }
    
    推荐文章