pathEndOrSingleSlash

Signature

def pathEndOrSingleSlash: Directive0 

Description

Only passes the request to its inner route if the unmatched path of the RequestContextRequestContext is either empty or contains only one single slash.

This directive is a simple alias for rawPathPrefix(Slash.? ~ PathEnd) and is mostly used on an inner-level to discriminate “path already fully matched” from other alternatives (see the example below). For a comparison between path directives check Overview of path directives.

It is equivalent to pathEnd | pathSingleSlash but slightly more efficient.

Example

Scala
sourceval route =
  pathPrefix("foo") {
    concat(
      pathEndOrSingleSlash {
        complete("/foo")
      },
      path("bar") {
        complete("/foo/bar")
      })
  }

// tests:
Get("/foo") ~> route ~> check {
  responseAs[String] shouldEqual "/foo"
}

Get("/foo/") ~> route ~> check {
  responseAs[String] shouldEqual "/foo"
}

Get("/foo/bar") ~> route ~> check {
  responseAs[String] shouldEqual "/foo/bar"
}
Java
sourceimport static org.apache.pekko.http.javadsl.server.Directives.complete;
import static org.apache.pekko.http.javadsl.server.Directives.path;
import static org.apache.pekko.http.javadsl.server.Directives.pathEndOrSingleSlash;
final Route route =
    concat(
        pathPrefix(
            "foo",
            () ->
                concat(
                    pathEndOrSingleSlash(() -> complete("/foo")),
                    path("bar", () -> complete("/foo/bar")))));
// tests:
testRoute(route).run(HttpRequest.GET("/foo")).assertEntity("/foo");
testRoute(route).run(HttpRequest.GET("/foo/")).assertEntity("/foo");
testRoute(route).run(HttpRequest.GET("/foo/bar")).assertEntity("/foo/bar");