

Route chaining, which tries a second route if a given first one was rejected.Route filtering, which only lets requests satisfying a given filter condition pass and rejects all others.Route transformation, which delegates processing to another, “inner” route but in the process changes some properties of either the incoming request, the outgoing response or both.There are three basic operations we need for building more complex routes from simpler ones: Usually you don’t create any RouteResult RouteResult instances yourself, but rather rely on the pre-defined RouteDirectives (like complete, reject or redirect) or the respective methods on the RequestContext instead. It is defined as such:įinal case class Complete(response: HttpResponse) extends RouteResultįinal case class Rejected(rejections: immutable.Seq) extends RouteResult RouteResult RouteResult is a simple algebraic data type (ADT) that models the possible non-error results of a Route Route. The RequestContext RequestContext itself is immutable but contains several helper methods which allow for convenient creation of modified copies. It also contains the unmatchedPath, a value that describes how much of the request URI has not yet been matched by a Path Directive. The request context wraps an HttpRequest HttpRequest instance to enrich it with additional information that are typically required by the routing logic, like an ExecutionContext, Materializer Materializer, LoggingAdapter LoggingAdapter and the configured RoutingSettings RoutingSettings. Note: There is also an implicit conversion from Route Route to Flow Flow defined in the RouteResult RouteResult companion, which relies on Route.toFlow. Using Route.toFlow or Route.toFunction a Route Route can be lifted into a handler Flow Flow or async handler function to be used with a bindAndHandleXXX call from the Core Server API. Sealing a Route is described more in detail later. You’ll see further down in the section about route composition what this is good for.Ī Route Route can be “sealed” using al, which relies on the in-scope RejectionHandler and ExceptionHandler ExceptionHandler instances to convert rejections and exceptions into appropriate HTTP responses for the client. In the second case “reject” means that the route does not want to handle the request. The first case is pretty clear, by calling complete a given response is sent to the client as reaction to the request. Do any kind of asynchronous processing and instantly return a Future CompletionStage to be eventually completed later.Fail the request by returning the value of requestContext.fail(.) or by just throwing an exception (see Exception Handling).Reject the request by returning the value of requestContext.reject(.) (see Rejections).


Generally when a route receives a request (or rather a RequestContext RequestContext for it) it can do one of these things: It also contains the current ExecutionContext and Materializer Materializer, so that these don’t have to be passed around manually. The RequestContext RequestContext is a data structure that contains the current request and auxiliary data like the so far unmatched path of the request URI that gets passed through the route structure. A Route Route itself is a function that operates on a RequestContext RequestContext and returns a RouteResult RouteResult.
