complete
Signature
def complete[T :ToResponseMarshaller](value: T): StandardRoute
def complete(response: HttpResponse): StandardRoute
def complete(status: StatusCode): StandardRoute
def complete[T :Marshaller](status: StatusCode, value: T): StandardRoute
def complete[T :Marshaller](status: Int, value: T): StandardRoute
def complete[T :Marshaller](status: StatusCode, headers: Seq[HttpHeader], value: T): StandardRoute
def complete[T :Marshaller](status: Int, headers: Seq[HttpHeader], value: T): StandardRoute
The signature shown is simplified, the real signature uses magnets. [1]
[1] See The Magnet Pattern for an explanation of magnet-based overloading.
Description
Completes the request using the given argument(s).
complete
uses the given arguments to construct a Route
Route
which simply calls complete
on the RequestContext
RequestContext
with the respective HttpResponse
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 the ones shown in the examples.
Example
- Scala
-
source
import org.apache.pekko import pekko.http.scaladsl.model._ import pekko.http.scaladsl.model.ContentTypes._ import pekko.http.scaladsl.model.headers.`Access-Control-Allow-Origin` val route = concat( path("a") { complete(HttpResponse(entity = "foo")) }, path("b") { complete(StatusCodes.OK) }, path("c") { complete(StatusCodes.Created -> "bar") }, path("d") { complete(201 -> "bar") }, path("e") { complete(StatusCodes.Created, List(`Access-Control-Allow-Origin`.`*`), "bar") }, path("f") { complete(201, List(`Access-Control-Allow-Origin`.`*`), "bar") }, path("g") { complete(Future { StatusCodes.Created -> "bar" }) }, path("h") & complete("baz") // `&` also works with `complete` as the 2nd argument ) // tests: Get("/a") ~> route ~> check { status shouldEqual StatusCodes.OK responseAs[String] shouldEqual "foo" } Get("/b") ~> route ~> check { status shouldEqual StatusCodes.OK responseAs[String] shouldEqual "OK" } Get("/c") ~> route ~> check { status shouldEqual StatusCodes.Created responseAs[String] shouldEqual "bar" } Get("/d") ~> route ~> check { status shouldEqual StatusCodes.Created responseAs[String] shouldEqual "bar" } Get("/e") ~> route ~> check { status shouldEqual StatusCodes.Created header[`Access-Control-Allow-Origin`] shouldEqual Some(`Access-Control-Allow-Origin`.`*`) responseAs[String] shouldEqual "bar" } Get("/f") ~> route ~> check { status shouldEqual StatusCodes.Created header[`Access-Control-Allow-Origin`] shouldEqual Some(`Access-Control-Allow-Origin`.`*`) responseAs[String] shouldEqual "bar" } Get("/g") ~> route ~> check { status shouldEqual StatusCodes.Created responseAs[String] shouldEqual "bar" } Get("/h") ~> route ~> check { status shouldEqual StatusCodes.OK responseAs[String] shouldEqual "baz" }
- Java
-
source
import static org.apache.pekko.http.javadsl.server.Directives.complete; import static org.apache.pekko.http.javadsl.server.Directives.path; final Route route = concat( 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");
1.1.0+17-3b5f9b27*