Configs2026年5月12日·1 分钟阅读

Spark Java — Micro Framework for Building Web APIs in Java

Spark is a micro framework for Java inspired by Sinatra, letting developers create web applications and REST APIs with minimal boilerplate using a simple lambda-based DSL.

Introduction

Spark Java (not to be confused with Apache Spark) is a micro web framework for Java 8+ that brings Sinatra-style simplicity to the JVM. It uses Java lambdas to define routes in just a few lines, making it a practical choice for small services, prototypes, and lightweight REST APIs where Spring Boot would be overkill.

What Spark Java Does

  • Defines routes using lambda expressions with a minimal DSL
  • Embeds a Jetty server so applications run standalone with no WAR deployment
  • Supports before/after filters for cross-cutting concerns like authentication
  • Renders HTML via pluggable template engines (Freemarker, Mustache, Thymeleaf)
  • Serves static files from the classpath or filesystem

Architecture Overview

Spark embeds Jetty as its HTTP server and maps incoming requests against a route table using method and path pattern matching. Each route is a lambda that receives a Request and Response object and returns the response body directly. Filters run before or after matched routes, and exception mappers convert thrown exceptions into HTTP responses. The entire framework fits in a small JAR with few dependencies.

Self-Hosting & Configuration

  • Add the Maven or Gradle dependency; no application server required
  • Set the port with port(8080) before defining routes
  • Configure TLS with secure("keystore.jks", "password", null, null)
  • Serve static files with staticFiles.location("/public") for classpath resources
  • Enable GZIP compression by setting response headers in an after-filter

Key Features

  • Entire route setup in under 10 lines of code with lambda syntax
  • Embedded Jetty server starts in under a second
  • WebSocket support via webSocket("/ws", handler)
  • Template engine integration for server-rendered pages
  • Tiny footprint with minimal transitive dependencies

Comparison with Similar Tools

  • Spring Boot — full enterprise framework with dependency injection, far heavier for simple APIs
  • Javalin — similar micro approach, built on Jetty, adds Kotlin support and OpenAPI
  • Micronaut — compile-time DI with low startup, more suited for microservice architectures
  • Vert.x — reactive and event-driven, higher throughput but steeper learning curve

FAQ

Q: Is Spark Java the same as Apache Spark? A: No. Spark Java is a web framework. Apache Spark is a big data processing engine. They share only the name.

Q: Does Spark Java support async or reactive programming? A: No. Spark uses a synchronous thread-per-request model via Jetty. For reactive workloads consider Vert.x or Spring WebFlux.

Q: Can Spark Java be used in production? A: Yes, for small to medium services. For large-scale enterprise applications with complex dependency graphs, a full framework is more appropriate.

Q: Does Spark Java support dependency injection? A: Not built-in. You can integrate Guice or Dagger manually if needed.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产