complete

complete

Description

Completes the request using the given argument(s).

complete uses the given arguments to construct a Route which simply calls complete on the RequestContext with the respective HttpResponse instance. Completing the request will send the response "back up" the route structure where all the logic runs that wrapping directives have potentially chained into the RouteResult future transformation chain.

Please note that the complete directive has multiple variants, like

Example

final Route route = route(
  path("a", () -> complete(HttpResponse.create().withEntity("foo"))),
  path("b", () -> complete(StatusCodes.OK)),
  path("c", () -> complete(StatusCodes.CREATED, "bar")),
  path("d", () -> complete(StatusCodes.get(201), "bar")),
  path("e", () ->
    complete(StatusCodes.CREATED,
             Collections.singletonList(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8)),
             HttpEntities.create("bar"))),
  path("f", () ->
    complete(StatusCodes.get(201),
             Collections.singletonList(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8)),
             HttpEntities.create("bar"))),
  path("g", () -> complete("baz"))
);

// tests:
testRoute(route).run(HttpRequest.GET("/a"))
  .assertStatusCode(StatusCodes.OK)
  .assertEntity("foo");

testRoute(route).run(HttpRequest.GET("/b"))
  .assertStatusCode(StatusCodes.OK)
  .assertEntity("OK");

testRoute(route).run(HttpRequest.GET("/c"))
  .assertStatusCode(StatusCodes.CREATED)
  .assertEntity("bar");

testRoute(route).run(HttpRequest.GET("/d"))
  .assertStatusCode(StatusCodes.CREATED)
  .assertEntity("bar");

testRoute(route).run(HttpRequest.GET("/e"))
  .assertStatusCode(StatusCodes.CREATED)
  .assertHeaderExists(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8))
  .assertEntity("bar");

testRoute(route).run(HttpRequest.GET("/f"))
  .assertStatusCode(StatusCodes.CREATED)
  .assertHeaderExists(ContentType.create(ContentTypes.TEXT_PLAIN_UTF8))
  .assertEntity("bar");

testRoute(route).run(HttpRequest.GET("/g"))
  .assertStatusCode(StatusCodes.OK)
  .assertEntity("baz");

Contents