How to apply route rejection in routes individullay

here is my route structure

classA {

implicit def classARejectionHandler =
    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(classARejectionHandler) {
concat(
        path("event-by-id") {
          get {
      }
} ,
  path("create-event") {
          post {
      }
}
}

classB {

implicit def classBRejectionHandler =
    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(classBRejectionHandler) {
      path("fileUploads") {
        post {
    }
}
}
}

class Demo extends App {

def main {
val a =new classA
val b = new ClassB

val routes = a~b
      val bindingFuture = Http().bindAndHandle(MainRouter.routes, hostName, port)

}


}

now the problem is when i hit the route localhost:8080/fileuploads with method POST with its params the route works when

  • i remove the rejections from classA

  • it works when i remove the rejection both from classA and classB

route does not work when

  • the rejection code is present in both classA and classB classes

  • only in classA

by does not work i means i get the response

Not here!

which is classA's handleNotFound rejection handler is rejecting the route of classsB because i am hitting the route of classB According to my understanding there is some problem in chaining the routes but i need to put different routes in different classes this is my requirement
how can i stop getting rejection of a desired route from other chained routes rejection handlers?

Try removing the handleNotFound clause. Otherwise, paths that are not found in class A will immediately be finally converted to “Not Found” and class B routes will have no chance at trying them.

if i remove it then it says The resource could not be found ,so what should be the work around ?

Sorry to hear that it still doesn’t work. It’s hard to say why without full code that compiles, though, probably something else is at play. If you can post a self-contained snippet that compiles someone will be able to help more likely.

Since you also posted about the “Method not supported” rejection in the other thread, note that the same advice is valid for all kinds of rejection handling (not just for path): whenever you finally handle a rejection a sibling route concatenated with concat or ~ will not be able to take over and complete the request later. For that reason, you should put the rejection handling code as far up in the routing tree as possible, maybe even only around the main route.

The routing tree gives some more general idea about the bigger picture and also the docs about [https://doc.akka.io/docs/akka-http/current/routing-dsl/rejections.html] might help. We are also going to improve the documentation about best-practices with how to structure route source code, see #2980.