как ограничить отклонение akka http желаемыми маршрутами - PullRequest
0 голосов
/ 26 марта 2020

у меня есть два класса, которые содержат маршруты

class A  {

implicit def myRejectionHandler =
RejectionHandler.newBuilder()
  .handle {
    case MissingCookieRejection(cookieName) =>
      complete(HttpResponse(BadRequest, entity = "No cookies, no service!!!"))
  }
  .handle {
    case AuthorizationFailedRejection =>
      complete((Forbidden, "You're out of your depth!"))
  }
  .handle {
    case ValidationRejection(msg, _) =>
      complete((InternalServerError, "That wasn't valid! " + msg))
  }
  .handleAll[MethodRejection] { methodRejections =>
    val names = methodRejections.map(_.supported.name)
    complete((MethodNotAllowed, s"Can't do that! Supported: ${names mkString " or "}!"))
  }
  .handleNotFound { complete((NotFound, "Not here!")) }
  .result()


val route: Route = handleRejections(myRejectionHandler) {
concat(
        path("event-by-id") {
          get {
            parameters('id.as[String]) {
              id =>
      complete("its working")
}
}
}
  path("create-event") {
          post {
            entity(as[Event]) {
              event =>
      completed("inserted")
}
}
}
)
}
}

class B {
implicit def myRejectionHandler =
RejectionHandler.newBuilder()
  .handle {
    case MissingCookieRejection(cookieName) =>
      complete(HttpResponse(BadRequest, entity = "No cookies, no service!!!"))
  }
  .handle {
    case AuthorizationFailedRejection =>
      complete((Forbidden, "You're out of your depth!"))
  }
  .handle {
    case ValidationRejection(msg, _) =>
      complete((InternalServerError, "That wasn't valid! " + msg))
  }
  .handleAll[MethodRejection] { methodRejections =>
    val names = methodRejections.map(_.supported.name)
    complete((MethodNotAllowed, s"Can't do that! Supported: ${names mkString " or "}!"))
  }
  .handleNotFound { complete((NotFound, "Not here!")) }
  .result()


    val route: Route = handleRejections(myRejectionHandler) {
path("fileUploads") {


  post {
        fileUploadAll("files") {

complete("file uploaded")
      }
}
}

}
}

class MainRouter extends App {

val a  = new A()
val b  = new B()
val routes = a.route ~ b.route
def main(args: Array[String]): Unit = {
    val bindingFuture = Http().bindAndHandle(routes, hostName, port)

}
}

, когда я нажимаю на маршрут POST 0.0.0.0:8080/fileUploads Это дает ответ

Not here!

вызывается обработчик HandleNotFound. Я отправляюсь в маршрут класса B, маршрут работает, когда я удаляю rejectionHandler из classA и ClassB

, если я удалить rejectionhandler из classB я получаю тот же ответ Not here!

, почему обработчик отклонения в class A применяется на маршрутах класса B? как я могу ограничить его собственными классами classA и classB

, маршрут работает только в том случае, если я удаляю обработчики отклонения как из classA, так и ClassB, пожалуйста, укажите, чего здесь не хватает

...