A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Spark 2.9.4 is out!!
<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
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.4</version>
</dependency>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:javadocThe result is put in /target/site/apidocs
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
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
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:
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.
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:
public class TransformerExample {
public static void main(String args[]) {
get("/hello", "application/json", (request, response) -> {
return new MyMessage("Hello World");
}, new JsonTransformer());
}
}See Spark-debug-tools as a separate module.
Is the project Dead?
SparkJava External Static Files Symlink Escape Allows Local File Read Outside Static Root
Job aborted due to stage failure
Is the Project dead?
java.util.ConcurrentModificationException from spark.route.Routes
Is the Spark Java framework compatible with Java 17
Adding route is not thread safe
Proper way to serve the same static file for multiple routes in the context of a single page application
Streaming back dynamically-generated content
Response body not possible (null) if HTTP status code is 304 (not modified).