Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

spark

> 编程语言
Open source

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

9.7K stars0 likes3 views
WebsiteGitHub

About

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

Spark - a tiny web framework for Java 8

Spark 2.9.4 is out!!

xml
<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-core</artifactId>
    <version>2.9.4</version>
</dependency>

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
<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-core</artifactId>
    <version>2.9.4</version>
</dependency>
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

…

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

…

Example showing a very simple (and stupid) authentication filter that is executed before all other resources

…

Example showing how to use attributes

…

Example showing how to serve static resources

java
import static spark.Spark.*;

public class StaticResources {

    public static void main(String[] args) {

        // Will serve all static file are under "/public" in classpath if the route isn't consumed by others routes.
        // When using Maven, the "/public" folder is assumed to be in "/main/resources"
        staticFileLocation("/public");

        get("/hello", (request, response) -> "Hello World!");
    }
}

Example showing how to define content depending on accept type

java
import static spark.Spark.*;

public class JsonAcceptTypeExample {

    public static void main(String args[]) {

        //Running curl -i -H "Accept: application/json" http://localhost:4567/hello json message is read.
        //Running curl -i -H "Accept: text/html" http://localhost:4567/hello HTTP 404 error is thrown.
        get("/hello", "application/json", (request, response) -> "{\"message\": \"Hello World\"}");
    }
} 

Example showing how to render a view from a template. Note that we are using ModelAndView class for setting the object and name/location of template.

First of all we define a class which handles and renders output depending on template engine used. In this case FreeMarker.

…

Then we can use it to generate our content. Note how we are setting model data and view name. Because we are using FreeMarker, in this case a Map and the name of the template is required:

java
public class FreeMarkerExample {

    public static void main(String args[]) {

        get("/hello", (request, response) -> {
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("message", "Hello FreeMarker World");

            // The hello.ftl file is located in directory:
            // src/test/resources/spark/examples/templateview/freemarker
            return modelAndView(attributes, "hello.ftl");
        }, new FreeMarkerTemplateEngine());
    }
}

Example of using Transformer.

First of all we define the transformer class, in this case a class which transforms an object to JSON format using gson API.

java
public class JsonTransformer implements ResponseTransformer {

	private Gson gson = new Gson();

	@Override
	public String render(Object model) {
		return gson.toJson(model);
	}
}

And then the code which return a simple POJO to be transformed to JSON:

java
public class TransformerExample {

    public static void main(String args[]) {
        get("/hello", "application/json", (request, response) -> {
            return new MyMessage("Hello World");
        }, new JsonTransformer());
    }
}

Debugging

See Spark-debug-tools as a separate module.

Issues· 262 open

View all issuesOpen on GitHub
  • #1291

    Is the project Dead?

    Updated Sep 28, 2026
  • #1296

    SparkJava External Static Files Symlink Escape Allows Local File Read Outside Static Root

    Updated Jun 18, 2026
  • #1289

    Job aborted due to stage failure

    Updated Nov 6, 2024
  • #1251

    Is the Project dead?

    Updated May 11, 2024
  • #1243

    java.util.ConcurrentModificationException from spark.route.Routes

    Updated Mar 22, 2024
  • #1275

    Is the Spark Java framework compatible with Java 17

    Updated Feb 15, 2024
  • #1287

    Adding route is not thread safe

    Updated Oct 22, 2023
  • #1286

    Proper way to serve the same static file for multiple routes in the context of a single page application

    Updated Sep 27, 2023
  • #1284

    Streaming back dynamically-generated content

    Updated Sep 12, 2023
  • #1267

    Response body not possible (null) if HTTP status code is 304 (not modified).

    Updated Jul 30, 2023

> Tags

Java

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言