pathSingleSlash

Signature

def pathSingleSlash: Directive0

Description

Only passes the request to its inner route if the unmatched path of the RequestContextRequestContext contains exactly one single slash.

This directive is a simple alias for pathPrefix(PathEnd) and is mostly used for matching requests to the root URI (/) on an inner-level to discriminate “all path segments matched” from other alternatives (see the example below). For a comparison between path directives check Overview of path directives.

Example

Scala
val route =
  pathSingleSlash {
    complete("root")
  } ~
    pathPrefix("ball") {
      pathSingleSlash {
        complete("/ball/")
      } ~
        path(IntNumber) { int =>
          complete(if (int % 2 == 0) "even ball" else "odd ball")
        }
    }

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

Get("/ball") ~> route ~> check {
  handled shouldEqual false
}

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

Get("/ball/1337") ~> route ~> check {
  responseAs[String] shouldEqual "odd ball"
}
Java
final Route route =
    route(
        pathSingleSlash(() -> complete("root")),
        pathPrefix("ball", () ->
            route(
                pathSingleSlash(() -> complete("/ball/")),
                path(integerSegment(), (i) -> complete((i % 2 == 0) ? "even ball" : "odd ball"))
            )
        )
    );
// tests:
testRoute(route).run(HttpRequest.GET("/")).assertEntity("root");
testRoute(route).run(HttpRequest.GET("/ball")).assertStatusCode(StatusCodes.NOT_FOUND);
testRoute(route).run(HttpRequest.GET("/ball/")).assertEntity("/ball/");
testRoute(route).run(HttpRequest.GET("/ball/1337")).assertEntity("odd ball");
The source code for this page can be found here.