perwendel /spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin

★ 9,627⑂ 1,568👁 392⑃ 532● 756▤ 0
JavaApache-2.0Created 2011-05-05
-1
日增长
9,627
stars
1,568
forks
392
watchers
532
prs
756
issues
0
releases

README

Spark - a tiny web framework for Java 8
==============================================

Spark 2.9.4 is out!!
xml

    com.sparkjava
    spark-core
    2.9.4



Sponsor the project here https://github.com/sponsors/perwendel

For documentation please go to: http://sparkjava.com/documentation

For usage questions, please use stack overflow with the “spark-java” tag 

Javadoc: http://javadoc.io/doc/com.sparkjava/spark-core

When committing to the project please use Spark format configured in https://github.com/perwendel/spark/blob/master/config/spark_formatter_intellij.xml

Getting started
---------------

xml

    com.sparkjava
    spark-core
    2.9.4



java
import static spark.Spark.;

public class HelloWorld {
    public static void main(String[] arg){
        get("/hello", (request, response) - "Hello World!");
    }
}


View at: http://localhost:4567/hello


Check out and try the examples in the source code.
You can also check out the javadoc. After getting the source from
github run: 

    mvn javadoc:javadoc

The result is put in /target/site/apidocs

Examples
---------

Simple example showing some basic functionality

java
import static spark.Spark.;

/
  A simple example just showing some basic functionality
 /
public class SimpleExample {

    public static void main(String[] args) {

        //  port(5678);  "Hello World!");

        post("/hello", (request, response) -
            "Hello World: " + request.body()
        );

        get("/private", (request, response) - {
            response.status(401);
            return "Go Away!!!";
        });

        get("/users/:name", (request, response) - "Selected user: " + request.params(":name"));

        get("/news/:section", (request, response) - {
            response.type("text/xml");
            return "" + request.params("section") + "";
        });

        get("/protected", (request, response) - {
            halt(403, "I don't think so!!!");
            return null;
        });

        get("/redirect", (request, response) - {
            response.redirect("/news/world");
            return null;
        });

        get("/", (request, response) - "root");
    }
}



-------------------------------

A simple CRUD example showing how to create, get, update and delete book resources

java
import static spark.Spark.;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/
  A simple CRUD example showing how to create, get, update and delete book resources.
 /
public class Books {

    /
      Map holding the books
     /
    private static Map books = new HashMap();

    public static void main(String[] args) {
        final Random random = new Random();

        // Creates a new book resource, will return the ID to the created resource
        // author and title are sent in the post body as x-www-urlencoded values e.g. author=Foo&title=Bar
        // you get them by using request.queryParams("valuename")
        post("/books", (request, response) - {
         

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON