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
val 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
import static akka.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("{}");
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.