web开发框架-spark

     Spark是一个微型的Java Web框架,它的灵感来自于Sinatra,它的目的是让你以最小的代价创建出一个Java Web应用。最近更新到2.0.0,支持Java 8 ,支持Lambda,Demo代码看起来非常有吸引力
spark 和Ruby语法相似,但是对web的支持是也是很好
import static spark.Spark.*;


public class HelloWorld {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World");
}
}
http://localhost:4567/hello
Getting started
Create a new maven project and add the dependency to your POM.xml:
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.3</version>
</dependency>
Not familiar with Maven? Click here for more detailed instructions.
Start coding:


import static spark.Spark.*;


public class HelloWorld {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World");
}
}
Run and view:


http://localhost:4567/hello


停止服务器:
我们可以调用stop方法来让服务器和所有的路由清除
Spark的应用程序的主要构建块是一组路由。路由是由三个部分组成:
一个动词(get, post, put, delete, head, trace, connect, options)
路径(/hello, /users/:name)
一个回调(request, response) -> { }
路由匹配的顺序定义。调用第一个匹配的路由请求。


get("/", (request, response) -> {
// .. Show something ..
});


post("/", (request, response) -> {
// .. Create something ..
});


put("/", (request, response) -> {
// .. Update something ..
});


delete("/", (request, response) -> {
// .. annihilate something ..
});


options("/", (request, response) -> {
// .. appease something ..
});


路由匹配包括命名参数(:parameter) ,访问这个参数方法的放入request代码块对象
路由匹配包括通配符的匹配 ,访问这个splat method的放入request代码块对象

// matches "GET /hello/foo" and "GET /hello/bar"
// request.params(":name") is 'foo' or 'bar'
get("/hello/:name", (request, response) -> {
return "Hello: " + request.params(":name");
});




// matches "GET /say/hello/to/world"
// request.splat()[0] is 'hello' and request.splat()[1] 'world'
支持通配符的匹配:*
get("/say/*/to/*", (request, response) -> {
return "Number of splat parameters: " + request.splat().length;
});
Request的对象常用的方法:


request.attributes();             // the attributes list
request.attribute("foo");         // value of foo attribute
request.attribute("A", "V");      // sets value of attribute A to V
request.body();                   // request body sent by the client
request.bodyAsBytes();            // request body as bytes
request.contentLength();          // length of request body
request.contentType();            // content type of request.body
request.contextPath();            // the context path, e.g. "/hello"
request.cookies();                // request cookies sent by the client
request.headers();                // the HTTP header list
request.headers("BAR");           // value of BAR header
request.host();                   // the host, e.g. "example.com"
request.ip();                     // client IP address
request.params("foo");            // value of foo path parameter
request.params();                 // map with all parameters
request.pathInfo();               // the path info
request.port();                   // the server port
request.protocol();               // the protocol, e.g. HTTP/1.1
request.queryMap();               // the query map
request.queryMap("foo");          // query map for a certain parameter
request.queryParams();            // the query param list
request.queryParams("FOO");       // value of FOO query param
request.queryParamsValues("FOO")  // all values of FOO query param
request.raw();                    // raw request handed in by Jetty
request.requestMethod();          // The HTTP method (GET, ..etc)
request.scheme();                 // "http"
request.servletPath();            // the servlet path, e.g. /result.jsp
request.session();                // session management
request.splat();                  // splat (*) parameters
request.uri();                    // the uri, e.g. "http://example.com/foo"
request.url();                    // the url. e.g. "http://example.com/foo"
request.userAgent();              // user agent
Response
In the handle method response information and functionality is provided by the response parameter:


response.body("Hello");        // sets content to Hello
response.header("FOO", "bar"); // sets header FOO with value bar
response.raw();                // raw response handed in by Jetty
response.redirect("/example"); // browser redirect to /example
response.status(401);          // set status code to 401
response.type("text/xml");     // set content type to text/xml
Query Maps
Query maps allows you to group parameters to a map by their prefix. This allows you to group two parameters like user[name] and user[age] to a user map.


request.queryMap().get("user", "name").value();
request.queryMap().get("user").get("name").value();
request.queryMap("user").get("age").integerValue();
request.queryMap("user").toMap();
Cookies
request.cookies();                              // get map of all request cookies
request.cookie("foo");                          // access request cookie by name
response.cookie("foo", "bar");                  // set cookie with a value
response.cookie("foo", "bar", 3600);            // set cookie with a max-age
response.cookie("foo", "bar", 3600, true);      // secure cookie
response.removeCookie("foo");                   // remove cookie
Sessions


每个方法的request,session 是否需要创建可以通过下面的方法来调用:
request.session(true)                            // create and return session
request.session().attribute("user")              // Get session attribute 'user'
request.session().attribute("user", "foo")       // Set session attribute 'user'
request.session().removeAttribute("user")        // Remove session attribute 'user'
request.session().attributes()                   // Get all session attributes
request.session().id()                           // Get session id
request.session().isNew()                        // Check is session is new
request.session().raw()                          // Return servlet object
Halting
To immediately stop a request within a filter or route use:
你可以通过 调用halt来停止的request请求:


halt();




halt(401);
Or the body:


halt("This is the body");
...or both:


halt(401, "Go away!");
Filters (过滤器)
前置过滤器会在每次请求前调用,也能读取修改后的response的对象
停止的执行可以执行halt(code,"msg");
before((request, response) -> {
boolean authenticated;
// ... check if authenticated
if (!authenticated) {
halt(401, "You are not welcome here");
}
});


后置过滤器会在每次请求前调用,也能读取修改后的response的对象
停止的执行可以执行halt(code,"msg");
After-filters are evaluated after each request, and can read the request and read/modify the response:


after((request, response) -> {
response.header("foo", "set by after filter");
});
过滤器也可以能通过通配符来执行的某些路径的过滤
before("/protected/*", (request, response) -> {
// ... check if authenticated
halt(401, "Go Away!");
});
Redirects
可以触发一个浏览器的进行跳转,也可以带入传入httpStatus来进行足协
response.redirect("/bar");
You can also trigger a browser redirect with specific http 3XX status code:


response.redirect("/bar", 301); // moved permanently
Exception Mapping(异常处理)
throw 来抛出异常
来操作所有的异常的来配置所有路由和过滤器的的异常的信息的的处理 
get("/throwexception", (request, response) -> {
throw new NotFoundException();
});
通过 exception()来设置相应的异常的返回的状态和信息 
exception(NotFoundException.class, (e, request, response) -> {
response.status(404);
response.body("Resource not found");
});
Static Files  (静态文件的访问)
你可以访问的静态文件通过staticFileLocation 方法的,可以直接  http://{host}:{port}/css/style.css
staticFileLocation("/public"); // Static files
你也可以能过扩展的静态文件方法来进行访问
externalStaticFileLocation("/var/www/public"); // Static files
ResponseTransformer (接口:结果转化器)
路由映射来通过转化器来输出到操作方法的,下面的方法可以通过Google gson 来进行json 解析
import com.google.gson.Gson;


public class JsonTransformer implements ResponseTransformer {


private Gson gson = new Gson();


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


}


你现在可以通过jsonTransFormer的来进行页面的json的解析


get("/hello", "application/json", (request, response) -> {
return new MyMessage("Hello World");
}, new JsonTransformer());


你也可以使用java8方法的 方法引用的来进行jsonTransfromer来进行转换 
Gson gson = new Gson();
get("/hello", (request, response) -> new MyMessage("Hello World"), gson::toJson);
Views and Templates (视图和模板)


一个模板和视图路由是建立在url匹配的上和模板引擎机制实现 相应的render方法


A TemplateViewRoute is built up by a path (for url-matching) and the template engine holding the implementation of the 'render' method. Instead of returning the result of calling toString() as body the TemplateViewRoute returns the result of calling render method.


The primary purpose of this kind of Route is to provide a way to create generic and reusable components for rendering output using a Template Engine.




Freemarker (渲染html用模板引擎)


Maven dependency:


<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-freemarker</artifactId>
<version>2.3</version>
</dependency>


Mustache 
Renders objects to HTML using the Mustache template engine.


Maven dependency:


<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-mustache</artifactId>
<version>2.3</version>
</dependency>
Velocity
Renders objects to HTML using the Velocity template engine.


Maven dependency:


<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-velocity</artifactId>
<version>2.3</version>
</dependency>
Source and example on GitHub
Thymeleaf
Renders objects to HTML using the Thymeleaf template engine.


Maven dependency:


<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-thymeleaf</artifactId>
<version>2.3</version>
</dependency>
Source and example on GitHub
Handlebars
Renders objects to HTML using the Handlebars template engine.


Maven dependency:


<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-handlebars</artifactId>
<version>2.3</version>
</dependency>
Source and example on GitHub
Jade
Renders objects to HTML using the Jade template engine.


Maven dependency:


<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-jade</artifactId>
<version>2.3</version>
</dependency>
Source and example on GitHub
Jetbrick
Renders objects to HTML using the Jetbrick template engine.


Maven dependency:


<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-jetbrick</artifactId>
<version>2.3</version>
</dependency>
Source and example on GitHub
Pebble
Renders objects to HTML using the Pebble template engine.


Maven dependency:


<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-pebble</artifactId>
<version>2.3</version>
</dependency>
Source and example on GitHub
Water
Renders objects to HTML using the Water template engine.


Maven dependency:


<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-water</artifactId>
<version>2.3</version>
</dependency>
Source and example on GitHub
Embedded web server
Standalone Spark runs on an embedded Jetty web server.




Port
By default, Spark runs on port 4567. If you want to set another port use port. This has to be done before using routes and filters:


port(9090); // Spark will run on port 9090
Secure(安全)
You can set the connection to be secure via the secure method. This has to be done before any route mapping:


secure(keystoreFile, keystorePassword, truststoreFile, truststorePassword);
ThreadPool
You can set the maximum number of threads easily:


int maxThreads = 8;
threadPool(maxThreads);
You can also configure the minimum numbers of threads, and the idle timeout:


int maxThreads = 8;
int minThreads = 2;
int timeOutMillis = 30000;
threadPool(maxThreads, minThreads, timeOutMillis);
Waiting for Initialization
You can use the method awaitInitialization() to check if the server is ready to handle requests. This is usually done in a separate thread, for example to run a health check module after your server has started. 
The method causes the current thread to wait until the embedded Jetty server has been initialized. Initialization is triggered by defining routes and/or filters. Hence, if you're using just one thread don't put this before you define your routes and/or filters.


awaitInitialization(); // Wait for server to be initialized
WebSockets
WebSockets provide a protocol full-duplex communication channel over a single TCP connection, meaning you can send message back and forth over the same connection.


WebSockets only works with the embedded Jetty server, and must be defined before regular HTTP routes. To create a WebSocket route, you need to provide a path and a handler class:


webSocket("/echo", EchoWebSocket.class);
init(); // Needed if you don't define any HTTP routes after your WebSocket routes
import org.eclipse.jetty.websocket.api.*;
import org.eclipse.jetty.websocket.api.annotations.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;


@WebSocket
public class EchoWebSocket {


// Store sessions if you want to, for example, broadcast a message to all users
private static final Queue<Session> sessions = new ConcurrentLinkedQueue<>();


@OnWebSocketConnect
public void connected(Session session) {
sessions.add(session);
}


@OnWebSocketClose
public void closed(Session session, int statusCode, String reason) {
sessions.remove(session);
}


@OnWebSocketMessage
public void message(Session session, String message) throws IOException {
System.out.println("Got: " + message);   // Print message
session.getRemote().sendString(message); // and send it back
}


}
Other web server
To run Spark on a web server (instead of the embedded jetty server), an implementation of the interface spark.servlet.SparkApplication is needed. You have to initialize the routes in the init() method, and the following filter has to be configured in your web.xml:


<filter>
<filter-name>SparkFilter</filter-name>
<filter-class>spark.servlet.SparkFilter</filter-class>
<init-param>
<param-name>applicationClass</param-name>
<param-value>com.company.YourApplication</param-value>
</init-param>
</filter>


<filter-mapping>
<filter-name>SparkFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
GZIP
GZIP is done automatically if it's in both the request and the response headers. This usually only means that you have to set it in your response headers.


If you want to GZIP a single response, you can add it manually to your route:


get("/some-path", (request, response) -> {
// code for your get
response.header("Content-Encoding", "gzip");
});
If you want to GZIP everything, you can use an after-filter


after((request, response) -> {
response.header("Content-Encoding", "gzip");
});
Javadoc
After getting the source from GitHub run:


mvn javadoc:javadoc
The result is put in /target/site/apidocs
Examples and Tutorials
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值