Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Static files

Static files are files the server sends as they are, without processing: HTML pages, CSS styles, images, JavaScript. Next to an API you often need at least a start page or a ready frontend. Javalin serves such files with one configuration line.

Where the files live

Create the src/main/resources/public folder and put two files in it:

<!-- src/main/resources/public/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blog</title>
    <link rel="stylesheet" href="/style.css">
</head>
<body>
<h1>Blog</h1>
<p>The API lives at <a href="/posts">/posts</a>.</p>
</body>
</html>
/* src/main/resources/public/style.css */
body { font-family: sans-serif; max-width: 40rem; margin: 2rem auto; }

Maven copies everything from src/main/resources into target/classes, that is, into the classpath — the set of folders and jar files Java loads classes and resources from. So the files also end up in the final jar.

Enabling it

Add a line to the configuration block:

// src/main/java/com/example/blog/Main.java
config.staticFiles.add("/public", Location.CLASSPATH);   // io.javalin.http.staticfiles.Location

The first argument is the folder inside the classpath, the second is where to look for it. At startup Javalin logs: Static file handler added: StaticFileConfig(hostedPath=/, directory=/public, location=CLASSPATH, ...).

The files are available from the site root: public/style.css opens at /style.css, and for / Javalin serves index.html:

curl -i http://localhost:8080/style.css
HTTP/1.1 200 OK
Content-Type: text/css
Cache-Control: max-age=0

body { font-family: sans-serif; max-width: 40rem; margin: 2rem auto; }

Javalin chose the content type from the file extension. It also adds an ETag header (omitted here): the browser uses it to tell whether the file has changed. Open http://localhost:8080 in a browser — you see the page with a link to the API.

Processing order

Javalin first looks for a route, and only if there is none does it look for a file. So the config.routes.get("/", ...) route from the first lesson hides index.html. Remove it so that / opens the page.

Settings

For finer settings there is a variant with a lambda:

PropertyWhat it sets
hostedPathThe address the files are served from, e.g. /assets
directoryThe folder with the files
locationLocation.CLASSPATH (inside the jar) or Location.EXTERNAL (a folder on disk)
headersHeaders for all files, e.g. caching
// src/main/java/com/example/blog/Main.java
config.staticFiles.add(files -> {
    files.hostedPath = "/assets";
    files.directory = "/public";
    files.location = Location.CLASSPATH;
});

With these settings the style is available at /assets/style.css.

Common mistake

If the given folder is not in the classpath, the server does not start:

Exception in thread "main" io.javalin.util.JavalinException: Static resource directory with path: '/static' does not exist. Depending on your setup, empty folders might not get copied to classpath.

Check that the folder name in the code matches the folder in src/main/resources and that it contains at least one file: Maven does not copy empty folders.

Thanks for reading!

Donate to the site — we work hard for you.