mapRouteResult

Signature

def mapRouteResult(f: RouteResult 

Description

Changes the message the inner route sends to the responder.

The mapRouteResult directive is used as a building block for Custom Directives to transform the RouteResult coming back from the inner route.

See Result Transformation Directives for similar directives.

Example

Scala
sourceval rejectAll = // not particularly useful directive
  mapRouteResult {
    case _ => Rejected(List(AuthorizationFailedRejection))
  }
val route =
  rejectAll {
    complete("abc")
  }

// tests:
Get("/") ~> route ~> check {
  rejections.nonEmpty shouldEqual true
}
Java
sourceimport static org.apache.pekko.http.javadsl.server.Directives.mapRouteResult;

import static org.apache.pekko.http.javadsl.server.Directives.complete;
import static org.apache.pekko.http.javadsl.server.Directives.mapRouteResult;

// this directive is a joke, don't do that :-)
final Route route =
    mapRouteResult(
        r -> {
          if (r instanceof Complete) {
            final HttpResponse response = ((Complete) r).getResponse();
            return RouteResults.complete(response.withStatus(200));
          } else {
            return r;
          }
        },
        () -> complete(StatusCodes.ACCEPTED));

// tests:
testRoute(route).run(HttpRequest.GET("/")).assertStatusCode(StatusCodes.OK);
final Route route =
    mapRouteResult(
        rr -> {
          final Iterable<Rejection> rejections =
              Collections.singletonList(Rejections.authorizationFailed());
          return RouteResults.rejected(rejections);
        },
        () -> complete("abc"));

// tests:
runRouteUnSealed(route, HttpRequest.GET("/"))
    .assertRejections(Rejections.authorizationFailed());