withSettings

Signature

def withSettings(settings: RoutingSettings): Directive0 

Description

Allows running an inner route using an alternative RoutingSettingsRoutingSettings in place of the default one.

The execution context can be extracted in an inner route using extractSettings directly, or used by directives which internally extract the materializer without surfacing this fact in the API.

Example

Scala
sourceval special = RoutingSettings(system).withFileGetConditional(false)

def sample() =
  path("sample") {
    // internally uses fileGetConditional setting
    getFromFile("example.json")
  }

val route =
  get {
    pathPrefix("special") {
      withSettings(special) {
        sample() // ETag/`If-Modified-Since` disabled
      }
    } ~ sample() // ETag/`If-Modified-Since` enabled
  }

// tests:
Post("/special/sample") ~> route ~> check {
  responseAs[String] shouldEqual s"{}"
}
Get("/sample") ~> route ~> check {
  responseAs[String] shouldEqual "{}"
}
Java
sourceimport static org.apache.pekko.http.javadsl.server.Directives.withSettings;

final RoutingSettings special =
    RoutingSettings.create(system().settings().config()).withFileGetConditional(false);

// internally uses fileGetConditional setting
final Route sample = path("sample", () -> getFromFile("example.json"));

final Route route =
    get(
        () ->
            Directives.concat(
                pathPrefix(
                    "special",
                    () ->
                        // ETag/`If-Modified-Since` disabled
                        withSettings(special, () -> sample)),
                sample // ETag/`If-Modified-Since` enabled
                ));

// tests:
testRoute(route).run(HttpRequest.GET("/special/sample")).assertEntity("{}");
testRoute(route).run(HttpRequest.GET("/sample")).assertEntity("{}");