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

时间:2015-03-12 11:28:38

标签: scala http spray

我有一个文件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)))
            }
          }
        }
 }

My 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并打印消息"未找到请求的资源"不应该是这样的!如果未定义该路径,是否应该进入处理程序?如果我注释掉rejectionHandler,一切都按预期工作。请帮帮我!

1 个答案:

答案 0 :(得分:1)

actionRoute将完成/因为它没有路径并且有handleRejections。这意味着它确实有/的路线,actionRoute ~ staticRoute永远不会#34;到staticRoute

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

handleRejections(myHandler) {
  actionsRoute ~ staticRoute
}
相关问题