|
| 1 | +# Static Resources |
| 2 | + |
| 3 | +[](https://mvnrepository.com/artifact/io.avaje/avaje-jex-static-content) |
| 4 | +[](https://javadoc.io/doc/io.avaje/avaje-jex-static-content) |
| 5 | + |
| 6 | +PLugin for serving static resources from the classpath or filesystem. |
| 7 | + |
| 8 | +It provides a `StaticContent` class to configure the location and HTTP path of your static resources, as well as other attributes. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +Add the static content dependency to your project: |
| 13 | +```xml |
| 14 | +<dependency> |
| 15 | + <groupId>io.avaje</groupId> |
| 16 | + <artifactId>avaje-jex-static-content</artifactId> |
| 17 | + <version>${avaje.jex.version}</version> |
| 18 | +</dependency> |
| 19 | +``` |
| 20 | + |
| 21 | +## Basic Usage |
| 22 | +```java |
| 23 | +StaticContent singleFile = |
| 24 | + StaticContent.ofFile("src/main/resources/example.txt").httpPath("/single").build(); |
| 25 | +StaticContent directoryCP = |
| 26 | + StaticContent.ofClassPath("/public/").httpPath("/").directoryIndex("index.html").build(); |
| 27 | +Jex app = |
| 28 | + Jex.create() |
| 29 | + .plugin(singleFile) // will serve the src/main/resources/example.txt |
| 30 | + .plugin(directoryCP); // will serve files from the /public classpath directory |
| 31 | +``` |
| 32 | + |
| 33 | +## Configuration Options |
| 34 | + |
| 35 | +| Method | Description | |
| 36 | +|--------|-------------| |
| 37 | +| `directoryIndex("index.html")` | The index file to be served when a directory is requested. | |
| 38 | +| `route("/public")` | Sets the HTTP path and security role for the static resource handler. | |
| 39 | +| `preCompress()` | Sent resources will be pre-compressed and cached in memory when this is enabled. | |
| 40 | +| `putMimeTypeMapping("sus", "application/sus")` | Adds a custom file extension MIME mapping to the configuration. | |
| 41 | +| `putResponseHeader("key", value)` | Adds a new response header to the configuration. | |
| 42 | +| `resourceLoader(clazz)` | Sets a custom resource loader for loading class/module path resources. | |
| 43 | +| `skipFilePredicate(ctx -> !ctx.path().contains("/skip"))` | Sets a predicate to filter files based on the request context. | |
0 commit comments