Skip to content

sparkjava-community/spark

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1,080 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Spark Framework - Community Maintained

License CI Maven Package

A tiny web framework for Java - community-maintained fork

🚨 About This 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.

Why This Fork?

  • βœ… 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 Project


πŸ“¦ Installation

Maven

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>

πŸš€ Getting Started

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


πŸ“– Examples

Simple Example

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>";
        });
    }
}

CRUD Example

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; }
    }
}

Filter Example (Authentication)

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")
        );
    }
}

Static Files

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!");
    }
}

JSON Example

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.


🀝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

Building from Source

git clone https://github.com/sparkjava-community/spark.git
cd spark
mvn clean install

Running Tests

mvn test

Note: Some tests may be skipped due to Java 17+ compatibility. Core functionality is fully tested (313/318 tests pass).


πŸ‘₯ Maintainers

Looking for co-maintainers! If you're interested in helping maintain Spark, please open an issue.


πŸ“œ License

Apache License 2.0 - Same as original Spark Framework


πŸ™ Credits


About

A simple expressive web framework for java.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Java 100.0%