-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
#549Support of event based (non-blocking) request processing.51 comments · 2016-05-09#208Providing Async method with Java 8 CompletableFuture44 comments · 2014-11-13#137Allow Jetty to be pluggable39 comments · 2014-05-01#1105Desired features/changes for Spark 3.029 comments · 2019-03-22#571Run before/after for static files24 comments · 2016-05-29#1251Is the Project dead?17 comments · 2021-12-13#386Internal Redirect?16 comments · 2015-11-10#375Server Sent Events Support *Enhancement16 comments · 2015-10-26