A tiny web framework for Java - community-maintained fork
Spark Framework was created by Per Wendel and last updated in 2021. This is a community-maintained fork that provides continued maintenance for the 800+ projects still using Spark.
- β Security updates - Updated Jetty (9.4.54) and SLF4J (2.0.12)
- β Java 17 compatibility - Works with modern Java versions
- β Bug fixes - Issues are addressed as discovered
- β 100% backward compatible - Drop-in replacement for Spark 2.9.4
- Original repository: perwendel/spark
- Original documentation: sparkjava.com
- Creator: Per Wendel
Add GitHub Packages repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>Add dependency:
<dependency>
<groupId>com.github.sparkjava-community</groupId>
<artifactId>spark</artifactId>
<version>2.9.4.2</version>
</dependency>import static spark.Spark.*;
public class HelloWorld {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World!");
}
}View at: http://localhost:4567/hello
- Documentation: sparkjava.com/documentation (original docs still apply)
- Javadoc: javadoc.io/doc/com.sparkjava/spark-core
- Questions: Stack Overflow - spark-java tag
- Issues: GitHub Issues
import static spark.Spark.*;
public class SimpleExample {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World!");
post("/hello", (req, res) -> "Hello World: " + req.body());
get("/users/:name", (req, res) ->
"Selected user: " + req.params(":name")
);
get("/news/:section", (req, res) -> {
res.type("text/xml");
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><news>"
+ req.params("section") + "</news>";
});
}
}import static spark.Spark.*;
import java.util.*;
public class Books {
private static Map<String, Book> books = new HashMap<>();
public static void main(String[] args) {
// Create
post("/books", (req, res) -> {
String author = req.queryParams("author");
String title = req.queryParams("title");
Book book = new Book(author, title);
String id = UUID.randomUUID().toString();
books.put(id, book);
res.status(201);
return id;
});
// Read
get("/books/:id", (req, res) -> {
Book book = books.get(req.params(":id"));
if (book != null) {
return "Title: " + book.getTitle()
+ ", Author: " + book.getAuthor();
} else {
res.status(404);
return "Book not found";
}
});
// Update
put("/books/:id", (req, res) -> {
String id = req.params(":id");
Book book = books.get(id);
if (book != null) {
String newAuthor = req.queryParams("author");
String newTitle = req.queryParams("title");
if (newAuthor != null) book.setAuthor(newAuthor);
if (newTitle != null) book.setTitle(newTitle);
return "Book updated";
} else {
res.status(404);
return "Book not found";
}
});
// Delete
delete("/books/:id", (req, res) -> {
Book book = books.remove(req.params(":id"));
if (book != null) {
return "Book deleted";
} else {
res.status(404);
return "Book not found";
}
});
}
static class Book {
private String author, title;
public Book(String author, String title) {
this.author = author;
this.title = title;
}
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
}
}import static spark.Spark.*;
import java.util.*;
public class FilterExample {
private static Map<String, String> users = new HashMap<>();
public static void main(String[] args) {
users.put("foo", "bar");
users.put("admin", "admin");
// Authentication filter
before((req, res) -> {
String user = req.queryParams("user");
String password = req.queryParams("password");
String dbPassword = users.get(user);
if (!(password != null && password.equals(dbPassword))) {
halt(401, "Unauthorized");
}
});
get("/hello", (req, res) -> "Hello World!");
after("/hello", (req, res) ->
res.header("X-Custom-Header", "Added by filter")
);
}
}import static spark.Spark.*;
public class StaticResources {
public static void main(String[] args) {
// Serve static files from /public in classpath
staticFileLocation("/public");
get("/hello", (req, res) -> "Hello World!");
}
}import static spark.Spark.*;
public class JsonExample {
public static void main(String[] args) {
get("/hello", "application/json", (req, res) ->
"{\"message\": \"Hello World\"}"
);
}
}For more examples, see the source code.
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
git clone https://github.com/sparkjava-community/spark.git
cd spark
mvn clean installmvn testNote: Some tests may be skipped due to Java 17+ compatibility. Core functionality is fully tested (313/318 tests pass).
- @KainoVaii - Maintainer, uses Spark in Obsidian Framework
Looking for co-maintainers! If you're interested in helping maintain Spark, please open an issue.
Apache License 2.0 - Same as original Spark Framework
- Original Creator: Per Wendel
- Original Contributors: All contributors
- Original Project: perwendel/spark