onSuccess

onSuccess

Description

Evaluates its parameter of type CompletionStage<T>, and once it has been completed successfully, extracts its result as a value of type T and passes it to the inner route.

If the future fails its failure throwable is bubbled up to the nearest ExceptionHandler.

To handle the Failure case manually as well, use onComplete, instead.

Example

final Route route = path("success", () ->
  onSuccess(() -> CompletableFuture.supplyAsync(() -> "Ok"),
    extraction -> complete(extraction)
  )
).orElse(path("failure", () ->
  onSuccess(() -> CompletableFuture.supplyAsync(() -> {
      throw new RuntimeException();
    }),
    extraction -> complete("never reaches here"))
));

testRoute(route).run(HttpRequest.GET("/success"))
  .assertEntity("Ok");

testRoute(route).run(HttpRequest.GET("/failure"))
  .assertStatusCode(StatusCodes.InternalServerError())
  .assertEntity("There was an internal server error.");

Contents