redirect

Description

Completes the request with a redirection response to a given target URI and of a given redirection type (status code).

redirect is a convenience helper for completing the request with a redirection response. It is equivalent to this snippet relying on the complete directive:

Example

final Route route = pathPrefix("foo", () ->
  route(
    pathSingleSlash(() -> complete("yes")),
    pathEnd(() -> redirect(Uri.create("/foo/"), StatusCodes.PERMANENT_REDIRECT))
  )
);

// tests:
testRoute(route).run(HttpRequest.GET("/foo/"))
  .assertEntity("yes");

testRoute(route).run(HttpRequest.GET("/foo"))
  .assertStatusCode(StatusCodes.PERMANENT_REDIRECT)
  .assertEntity("The request, and all future requests should be repeated using <a href=\"/foo/\">this URI</a>.");
The source code for this page can be found here.