如何在 Akka HTTP 中嵌套路由?
How to nest routes in Akka HTTP?
我正在尝试编写一系列简单的路线,这是我想要发生的事情:
GET /
应该打印 "hello get"
POST /
应该打印 "hello post"
GET /foo
应该打印 "hello foo get"
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"))
}
}
这适用于 GET /
和 POST /
但 GET 和 POST 都适用于 /foo
404。
我几乎尝试了所有方法,但不知道该怎么做。当涉及到这个时,文档很难理解。
谁能给我指点一下?
你能试试这个吗?它对我有用。
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
并在路由绑定语句中使用 finalRoute。
val bindingFuture = Http().bindAndHandle(finalRoute, "localhost", 8085)
我建议以这种方式构建路径以获得最大的可读性:
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")
}
我正在尝试编写一系列简单的路线,这是我想要发生的事情:
GET /
应该打印 "hello get"
POST /
应该打印 "hello post"
GET /foo
应该打印 "hello foo get"
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"))
}
}
这适用于 GET /
和 POST /
但 GET 和 POST 都适用于 /foo
404。
我几乎尝试了所有方法,但不知道该怎么做。当涉及到这个时,文档很难理解。
谁能给我指点一下?
你能试试这个吗?它对我有用。
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
并在路由绑定语句中使用 finalRoute。
val bindingFuture = Http().bindAndHandle(finalRoute, "localhost", 8085)
我建议以这种方式构建路径以获得最大的可读性:
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")
}