mapUnmatchedPath
Description
Transforms the unmatchedPath field of the request context for inner routes.
The mapUnmatchedPath
directive is used as a building block for writing Custom Directives. You can use it for implementing custom path matching directives.
Use extractUnmatchedPath
for extracting the current value of the unmatched path.
Example
- Scala
-
source
def ignore456(path: Uri.Path) = path match { case s @ Uri.Path.Segment(head, tail) if head.startsWith("456") => val newHead = head.drop(3) if (newHead.isEmpty) tail else s.copy(head = head.drop(3)) case _ => path } val ignoring456 = mapUnmatchedPath(ignore456) val route = pathPrefix("123") { ignoring456 { path("abc") { complete("Content") } } } // tests: Get("/123/abc") ~> route ~> check { responseAs[String] shouldEqual "Content" } Get("/123456/abc") ~> route ~> check { responseAs[String] shouldEqual "Content" }
- Java
-
source
import static org.apache.pekko.http.javadsl.server.Directives.mapUnmatchedPath; final Function<String, String> ignore456 = path -> { int slashPos = path.indexOf("/"); if (slashPos != -1) { String head = path.substring(0, slashPos); String tail = path.substring(slashPos); if (head.length() <= 3) { return tail; } else { return path.substring(3); } } else { return path; } }; final Route route = pathPrefix( "123", () -> mapUnmatchedPath(ignore456, () -> path("abc", () -> complete("Content")))); // tests: testRoute(route).run(HttpRequest.GET("/123/abc")).assertEntity("Content"); testRoute(route).run(HttpRequest.GET("/123456/abc")).assertEntity("Content");
1.0.1