getFromResource
Signature
def getFromResource(resourceName: String)(implicit resolver: ContentTypeResolver): Route
def getFromResource( resourceName: String, contentType: ContentType, classLoader: ClassLoader Description
Completes GET requests with the content of the given classpath resource.
For details refer to getFromFile which works the same way but obtaining the file from the filesystem instead of the applications classpath.
A resource name that contains a .. segment, a backslash or a NUL character is rejected, unless the pekko.http.routing.reject-unsafe-resource-names setting is turned off. The directive does not otherwise know which prefix a name was built from: validate any part of the name that comes from the request, or use getFromResourceDirectory, which serves only resources below the given directory.
Note that it’s not required to wrap this directive with get as this directive will only respond to GET requests.
Example
- Scala
-
source
import org.apache.pekko.http.scaladsl.server.directives._ import ContentTypeResolver.Default val route = path("logs" / Segment) { name => getFromResource(s"$name.log") // uses implicit ContentTypeResolver } // tests: Get("/logs/example") ~> route ~> check { responseAs[String] shouldEqual "example file contents" } - Java
-
source
import static org.apache.pekko.http.javadsl.server.Directives.getFromResource; import static org.apache.pekko.http.javadsl.server.Directives.path; final Route route = path(PathMatchers.segment("logs").slash(segment()), name -> getFromResource(name + ".log")); // tests: testRoute(route).run(HttpRequest.GET("/logs/example")).assertEntity("example file contents");
2.0.0