喷雾拒绝处理程序未检测到现有路径

Spray rejection handler not detecting an existing path

我有一个文件 static.scala :

  val staticRoute = {
    path("") {
      getFromResource("web/index.html")
    } ~ pathPrefix("web") {
      getFromResourceDirectory("web")
    }
  }

另一个文件 a.scala :

    val actionRoute = (handleRejections(rejectionHandler) & handleExceptions(exceptionHandler))
      {
        path("action" / "create") {
          (put | post) {
            implicit ctx => {
              val xmlString = ctx.request.entity.asString
              val actionObject = XML.loadString(xmlString).toAction
              ActionService.write(actionObject)
              sendResponse(StatusCodes.OK, APIResponseOK(Map("id" -> actionObject.id)))
            }
          }
        }
 }

我的 Base.scala 包含 rejectionHandler 的定义:

  val rejectionHandler = RejectionHandler {

    case Nil => ctx => {
      complete((StatusCodes.NotFound, "The Requested Resource was not found"))
    }

    case mcr : MissingCookieRejection =>ctx=> { //testing
      complete(StatusCodes.BadRequest, "Missing cookie")
    }
  }

这两个文件都扩展了 Base.scala,其中定义了拒绝处理程序。但是,在为对应于

的服务器 ( localhost:8080 ) 打开正确的端口时
 path("") 

static.scala 中,rejectionHandler 仍将大小写为 Nil 并打印消息 "The Requested Resource was not found",但事实并非如此!如果未定义该路径,它不应该进入处理程序吗?如果我注释掉 rejectionHandler,一切都会按预期进行。请帮帮我!

actionRoute 将完成 /,因为它没有路径,但有一个 handleRejections。这意味着它确实有 / 的路线,并且 actionRoute ~ staticRoute 永远不会 "fall through" 到 staticRoute.

您通常只想在最顶层进行拒绝处理(或者可能在 pathPrefix 内,如果您不希望其他路由使用相同的前缀)。将 handleRejections 移出 actionRoute 并将其向上移动到顶层:

handleRejections(myHandler) {
  actionsRoute ~ staticRoute
}