Fork me on GitHub

SparkJava and Javalin comparison

People often ask about the differences between Spark and Javalin. This page shows some of them. It’s not intended to tell you which library is better, just to highlight the differences.

Handlers vs Routes and Filters

Javalin has the concept of a Handler. A Handler is void and takes a Context (which wraps HttpServletRequest and HttpServletResponse), and you operate on both the request and response through this Context.

app.get("/path", ctx -> ctx.result("Hello, World!"));
app.after("/path", ctx -> ctx.result("Actually, nevermind..."));

Spark on the other hand has Routes and Filters. Both Route and Filter in Spark take (Request, Response) as input. Route has a return value (Object), while Filter is void.

Request and Response wrap HttpServletRequest and HttpServletResponse, respectively.

app.get("/path", (req, res) -> "Hello, World!");
app.after("/path", (req, res) -> res.body("Actually, nevermind..."));

Some other differences:

Do you like Javalin?