pathSingleSlash
Description
Only passes the request to its inner route if the unmatched path of the RequestContext
RequestContext
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
-
source
val route = concat( pathSingleSlash { complete("root") }, pathPrefix("ball") { concat( 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
-
source
import 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.pathPrefix; import static org.apache.pekko.http.javadsl.server.Directives.pathSingleSlash; final Route route = concat( pathSingleSlash(() -> complete("root")), pathPrefix( "ball", () -> concat( 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");
1.1.0