【问题标题】:Spray rejection handler not detecting an existing path喷涂拒绝处理程序未检测到现有路径
【发布时间】:2015-03-12 11:28:38
【问题描述】:

我有一个文件 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 并打印消息“未找到请求的资源”,这不应该是这种情况!如果未定义该路径,它不应该进入处理程序吗?如果我注释掉拒绝处理程序,一切都会按预期工作。请帮帮我!

【问题讨论】:

    标签: scala http spray


    【解决方案1】:

    actionRoute 将完成 /,因为它没有路径并且确实有 handleRejections。这意味着它确实有一条通往/ 的路线,而actionRoute ~ staticRoute 永远不会“落入”staticRoute

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

    handleRejections(myHandler) {
      actionsRoute ~ staticRoute
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 2016-01-29
      相关资源
      最近更新 更多