我无法到达我的会话路由,我的 akka-http 应用程序中的路由 DSL 代码出现问题

I am not able to reach my sessions route, issue with my routing DSL code in my akka-http app

我可以使用以下方式访问登机路线:

wget http://127.0.0.1:9000/board

现在,如果我尝试像这样访问我的会话路由:

wget http://127.0.0.1:9000/session/getByToken

我收到这个错误:

--2021-11-30 11:56:12-- http://localhost:9000/session/getByToken Resolving localhost (localhost)... 127.0.0.1 Connecting to localhost (localhost)|127.0.0.1|:9000... connected. HTTP request sent, awaiting response... 404 Not Found 2021-11-30 11:56:12 ERROR 404: Not Found.

我的路由配置有什么问题?

此外,我希望路由最终被这样调用:

wget http://127.0.0.1:9000/session/getByToken/abc123abc123abc1234

如何在 getByToken 路由上获取字符串参数?

 val boardRoutes =
    path("board") {
        get {
        complete("board#index route")
        }
    }

    val sessionRoutes =
    concat(
        path("session") {
        path("getByToken") {
            get {
            complete("session#load route TODO add param for tokenId")
            }
        }
        path("login") {
            post {
            complete("session#login route")
            }
        }
        path("logout") {
            delete {
            complete("session#login route")
            }
        }
        }
    )

    val routes = chatRoute ~ boardRoutes ~ sessionRoutes

A path 次尝试匹配剩余的完整路径,嵌套路径将被忽略。相反,您需要 pathPrefix 作为最外层的指令。 Remaining 提取可用于匹配 tokenId 的路径的其余部分。

pathPrefix("session") {
  path("getByToken" / Remaining) { tokenId =>
    get {
      complete("session#load route TODO add param for "+ tokenId)
    }
  } ~
    path("login") {
      post {
        complete("session#login route")
      }
    } ~
    path("logout") {
      delete {
        complete("session#login route")
      }
    }
}

此外,concat 使用 foldLeft 链接指令,因此可以这样使用:

pathPrefix("session") {
  concat(
    path("getByToken" / Remaining) { tokenId =>
      get {
        complete("session#load route TODO add param for "+ tokenId)
      }
    },
    path("login") {
      post {
        complete("session#login route")
      }
    },
    path("logout") {
      delete {
        complete("session#login route")
      }
    }
  )
}