The routes and their replies

A route is a function from a path to a status and a body. This page writes that function and prints its answers for six paths, before any socket exists. The server of the next page is then a thin adapter: read the path from the request, call the function, write the reply. That order is deliberate, because a function is easy to test and a socket is not.

The reply record

Reply has a status and a body and nothing else, and it is a record because the pair travels together. The status is an int and not an enum: HTTP has more statuses than a service uses, and the three this one answers are 200, 400 and 404.

The path table

Four paths are answered and everything else is a 404. The order matters: /loans/overdue is compared before the /loans/ prefix, or the word overdue would be read as an id and answered with a 400. The comparisons put the literal first ("/health".equals(path)), which is the style PMD asks for and is safe when the path is null.

package library; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; public class Desk { record Loan(int id, String member, String title, LocalDate due, LocalDate returned) { State state(LocalDate today) { if (returned != null) { return new Returned(returned); } long days = ChronoUnit.DAYS.between(due, today); if (days > 0) { return new Overdue(days); } if (days >= -2) { return new DueSoon(-days); } return new OnLoan(-days); } String toJson(LocalDate today) { return "{" + field("id", id) + "," + field("member", member) + "," + field("title", title) + "," + field("due", due.toString()) + "," + Q + "state" + Q + ":" + stateJson(state(today)) + "}"; } } sealed interface State permits OnLoan, DueSoon, Overdue, Returned { } record OnLoan(long daysLeft) implements State { } record DueSoon(long daysLeft) implements State { } record Overdue(long daysLate) implements State { } record Returned(LocalDate on) implements State { } static final LocalDate TODAY = LocalDate.of(2026, 11, 20); static final List<Loan> LOANS = List.of( new Loan(1, "Ada", "Dune", LocalDate.of(2026, 11, 25), null), new Loan(2, "Grace", "The Dispossessed", LocalDate.of(2026, 11, 21), null), new Loan(3, "Alan", "Snow Crash", LocalDate.of(2026, 11, 14), null), new Loan(4, "Edsger", "Programming Pearls", LocalDate.of(2026, 11, 10), LocalDate.of(2026, 11, 18))); static final String Q = String.valueOf((char) 34); static final String B = String.valueOf((char) 92); static String field(String name, String value) { return Q + name + Q + ":" + quote(value); } static String field(String name, int value) { return Q + name + Q + ":" + value; } static String quote(String text) { StringBuilder out = new StringBuilder(Q); for (char c : text.toCharArray()) { if (c == 34) { out.append(B).append(Q); } else if (c == 92) { out.append(B).append(B); } else { out.append(c); } } return out.append(Q).toString(); } static String stateJson(State state) { return switch (state) { case OnLoan onLoan -> "{" + field("kind", "on_loan") + "," + field("days", (int) onLoan.daysLeft()) + "}"; case DueSoon dueSoon -> "{" + field("kind", "due_soon") + "," + field("days", (int) dueSoon.daysLeft()) + "}"; case Overdue overdue -> "{" + field("kind", "overdue") + "," + field("days", (int) overdue.daysLate()) + "}"; case Returned returned -> "{" + field("kind", "returned") + "," + field("on", returned.on().toString()) + "}"; }; } static String array(List<Loan> loans, LocalDate today) { StringBuilder out = new StringBuilder("["); for (int i = 0; i < loans.size(); i++) { if (i > 0) { out.append(','); } out.append(loans.get(i).toJson(today)); } return out.append(']').toString(); } record Reply(int status, String body) { } static List<Loan> overdue() { List<Loan> result = new ArrayList<>(); for (Loan loan : LOANS) { if (loan.state(TODAY) instanceof Overdue) { result.add(loan); } } return result; } static Reply route(String path) { if ("/health".equals(path)) { return new Reply(200, "{" + field("status", "ok") + "," + field("loans", LOANS.size()) + "}"); } if ("/loans".equals(path)) { return new Reply(200, array(LOANS, TODAY)); } if ("/loans/overdue".equals(path)) { return new Reply(200, array(overdue(), TODAY)); } if (path.startsWith("/loans/")) { try { int id = Integer.parseInt(path.substring("/loans/".length())); for (Loan loan : LOANS) { if (loan.id() == id) { return new Reply(200, loan.toJson(TODAY)); } } } catch (NumberFormatException e) { return new Reply(400, "{" + field("error", "bad id") + "}"); } return new Reply(404, "{" + field("error", "no such loan") + "}"); } return new Reply(404, "{" + field("error", "no route") + "}"); } public static void main(String[] args) { for (String path : List.of("/health", "/loans", "/loans/2", "/loans/overdue", "/loans/9", "/missing")) { Reply reply = route(path); System.out.println("GET " + path + " -> " + reply.status() + " " + reply.body()); } } }

The overdue filter

The overdue route is a filter over the same list, and it is one line of pattern matching: loan.state(TODAY) instanceof Overdue. The sealed interface makes the test total: the compiler knows the four forms, and the filter names the one it wants without a cast. A boolean field would have made this an if that a fifth state could invalidate.

200, 404 and 400

The three answers have three shapes. A 200 carries the loan or the list. A 404 carries {"error":"no such loan"} when the id is a number that is not in the desk, and {"error":"no route"} when the path matches nothing. A 400 carries {"error":"bad id"} when the part after /loans/ is not a number at all. An error has a body as well as a status, because a client that logs the body can say what went wrong.

The run

The main prints the six paths in the order of the list, each with the status and the body the route answers. The long line is the list of all four loans:

javac -Xlint:all -d out src/library/Desk.java
java -cp out library.Desk

GET /health -> 200 {"status":"ok","loans":4}
GET /loans -> 200 [{"id":1,"member":"Ada","title":"Dune","due":"2026-11-25","state":{"kind":"on_loan","days":5}},{"id":2,"member":"Grace","title":"The Dispossessed","due":"2026-11-21","state":{"kind":"due_soon","days":1}},{"id":3,"member":"Alan","title":"Snow Crash","due":"2026-11-14","state":{"kind":"overdue","days":6}},{"id":4,"member":"Edsger","title":"Programming Pearls","due":"2026-11-10","state":{"kind":"returned","on":"2026-11-18"}}]
GET /loans/2 -> 200 {"id":2,"member":"Grace","title":"The Dispossessed","due":"2026-11-21","state":{"kind":"due_soon","days":1}}
GET /loans/overdue -> 200 [{"id":3,"member":"Alan","title":"Snow Crash","due":"2026-11-14","state":{"kind":"overdue","days":6}}]
GET /loans/9 -> 404 {"error":"no such loan"}
GET /missing -> 404 {"error":"no route"}

Why routing is a function

A handler that parsed the request, chose a branch and wrote the response in one method cannot be tested without a socket, and a test that opens a socket for every case is slow and flaky. Splitting the decision from the transport gives two things: a pure function that the tests call directly, and a handler of four lines that the one socket test covers. It is the same split capstone 3 made when it moved the routing out of the handler.

The function is not pure in the strict sense: it reads the static list. That is the desk, and the desk is fixed for the demonstration. A service that changed its data would pass the store as a parameter, and the page would show a field instead of a static. The route and the reply would not change.

The next page

The next page adds HttpServer: the context, the handler that calls this function, the bytes of the body and the self test that asks four paths over a socket. The Java course covers the same split in lesson 134 and the string switch in lesson 014.

The code on this page was compiled with OpenJDK 21.0.13 (javac) and run on Alpine Linux in a Docker container. The date of the demonstration is fixed, so the output is the same on every run.

Article author: Arthur Isaev