【发布时间】:2020-03-26 17:39:24
【问题描述】:
我有以下来自https://doc.akka.io/docs/akka-http/current/routing-dsl/rejections.html的akka http拒绝处理代码
val message = "The requested resource could not be found."
implicit def myRejectionHandler = RejectionHandler.newBuilder()
.handleNotFound {
complete(HttpResponse(NotFound
,entity = HttpEntity(ContentTypes.`application/json`, s"""{"rejection": "$message"}"""
)))
}.result()
val route: Route = handleRejections(myRejectionHandler) {
handleExceptions(myExceptionHandler) {
concat(
path("event-by-id") {
get {
parameters('id.as[String]) {
id =>
complete("id")
}
}
}
,
post {
path("create-event") {
entity(as[Event]) {
event =>
complete(OK, "inserted")
}
}
}
)
}
}
}
val bindingFuture = Http().bindAndHandle(route, hostName, port)
当我点击localhost:8080/random时
我收到了消息
HTTP method not allowed, supported methods: POST
当我选择POST 并点击localhost:8080/random
我收到了消息
{
"rejection": "The requested resource could not be found."
}
为什么当我的路由请求是 GET 时我没有收到相同的消息?
在文档中,handleNotFound 正在处理 GET 请求 https://doc.akka.io/docs/akka-http/current/routing-dsl/rejections.html
【问题讨论】: