Java中是否有类似ExpressJS的轻量级Web框架?
Absolutely! The Java ecosystem has several lightweight, Express-like web frameworks that check all your boxes—handling HTTP requests, sending responses, managing cookies, and avoiding the heavyweight overhead of full-stack frameworks like Spring Boot (though Spring Boot can be trimmed down, these are purpose-built for minimalism). Here are the top picks you should explore:
Spark Java (not Apache Spark!)
Spark Java is probably the closest you’ll get to Express in the Java world—it’s designed for simplicity, with a fluent, route-based API that feels almost identical to Express. It runs on Jetty under the hood, supports all HTTP methods, and has straightforward cookie handling.
Quick example:
import static spark.Spark.*; public class Main { public static void main(String[] args) { // Basic GET route get("/hello", (req, res) -> { // Set a cookie res.cookie("user_session", "abc123", 3600); // Expires in 1 hour return "Hello from Spark Java!"; }); // Route that reads a cookie get("/profile", (req, res) -> { String sessionId = req.cookie("user_session"); return "Your session ID: " + sessionId; }); } }
Javalin
Javalin is a modern, lightweight framework built on Jetty (like Spark) but with a more polished, maintainable API. It’s focused on developer experience, supports async endpoints, and has excellent cookie management (including secure, HttpOnly cookies). It’s also more actively maintained than Spark these days.
Quick example:
import io.javalin.Javalin; public class Main { public static void main(String[] args) { var app = Javalin.create().start(7000); // GET route with response and cookie app.get("/greet", ctx -> { ctx.cookie("theme", "dark", 86400); // 1 day expiry ctx.result("Hello from Javalin!"); }); // Read cookie in a route app.get("/settings", ctx -> { String theme = ctx.cookie("theme"); ctx.result("Your preferred theme: " + (theme != null ? theme : "light")); }); } }
Vert.x Web
If you’re looking for a reactive, high-performance option that’s still lightweight, Vert.x Web is a great choice. It’s part of the Eclipse Vert.x ecosystem, which is built for non-blocking I/O. While its API is a bit different from Express, it’s incredibly flexible, supports all HTTP features (including cookies), and can handle high traffic loads with minimal resources.
Quick example:
import io.vertx.core.Vertx; import io.vertx.ext.web.Router; public class Main { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); Router router = Router.router(vertx); // GET route with cookie router.get("/welcome").handler(ctx -> { ctx.addCookie(ctx.cookieBuilder() .setName("visited") .setValue("true") .setMaxAge(604800) // 1 week .build()); ctx.response().end("Welcome to Vert.x Web!"); }); // Read cookie router.get("/status").handler(ctx -> { String visited = ctx.getCookie("visited") != null ? "yes" : "no"; ctx.response().end("Have you visited before? " + visited); }); vertx.createHttpServer().requestHandler(router).listen(8080); } }
All three of these frameworks are perfect for building lightweight web services, APIs, or even small web apps—they avoid the boilerplate of heavier frameworks while giving you all the tools you need to handle HTTP requests, responses, cookies, and more.
内容的提问来源于stack exchange,提问作者Simon

