Skip to main content

Static files

The BasicRestServlet and BasicRestObject classes come with built-in support for serving up static files through the following REST operation:

@RestGet(path="/htdocs/*")
public HttpResource getHtdoc(@Path("/*") String path, Locale locale) throws NotFound {
return getContext().getStaticFiles().resolve(path, locale).orElseThrow(NotFound::new);
}

The static file finder can be accessed through the following methods:

RestContextgetStaticFiles()RestRequestgetStaticFiles()

By default, the StaticFiles bean is configured as follows:

StaticFiles
.create()
.beanStore(beanStore) // Allow injected beans in constructor.
.type(BasicStaticFiles.class) // Default implementation class.
.dir("static") // Look in working /static directory.
.dir("htdocs") // Look in working /htdocs directory.
.cp(resourceClass, "htdocs", true) // Look in htdocs subpackage.
.cp(resourceClass, "/htdocs", true) // Look in htdocs package.
.caching(1_000_000) // Cache files in memory up to 1MB.
.exclude("(?i).*\\.(class|properties)") // Ignore class/properties files.
.headers(cacheControl("max-age=86400, public")); // Add cache control.

Static files can be configured programmatically through the following APIs:

RestContext.BuilderstaticFiles(StaticFiles)staticFiles()