getFromResourceDirectory

Signature

def getFromResourceDirectory(directoryName: String, classLoader: ClassLoader = _defaultClassLoader)(implicit resolver: ContentTypeResolver): Route

Description

Completes GET requests with the content of the given classpath resource directory.

For details refer to getFromDirectory which works the same way but obtaining the file from the filesystem instead of the applications classpath.

Note that it’s not required to wrap this directive with get as this directive will only respond to GET requests.

Example

Scala
sourceval route =
  pathPrefix("examples") {
    getFromResourceDirectory("examples")
  }

// tests:
Get("/examples/example-1") ~> route ~> check {
  responseAs[String] shouldEqual "example file contents"
}
Java
sourceimport static akka.http.javadsl.server.Directives.getFromResourceDirectory;
import static akka.http.javadsl.server.Directives.pathPrefix;

final Route route = pathPrefix("examples", () ->
  getFromResourceDirectory("/examples")
);

// tests:
testRoute(route).run(HttpRequest.GET("/examples/example-1"))
  .assertEntity("example file contents");
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.