Spark 是一个基于Java的微Web框架

14-06-07 banq
              

Spark是一个可像Node.js的Express那样快速Web框架,它虽然和大数据处理框架Spark同名,但是它是基于Java的,受Ruby的Sinatra框架鼓舞,用于Java的Web快速开发,使用Java 8 Lambda编写。

开启一个带URL的服务代码如下:

import static spark.Spark.*;

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

通过浏览器打开http://localhost:4567/hello

就会看到Hello world

实现RESTful架构也非常简单,如下:

import static spark.Spark.*;

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

import spark.Request;
import spark.Response;
import spark.Route;

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

    /**
     * Map holding the books
     */
    private static Map<String, Book> books = new HashMap<String, Book>();

    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 as query parameters e.g. /books?author=Foo&title=Bar
        post("/books", (request, response) -> {

                String author = request.queryParams("author");
                String title = request.queryParams("title");
                Book book = new Book(author, title);

                int id = random.nextInt(Integer.MAX_VALUE);
                books.put(String.valueOf(id), book);

                response.status(201); // 201 Created
                return id;
        });

        // Gets the book resource for the provided id
        get("/books/:id", (request, response) -> {
                Book book = books.get(request.params(":id"));
                if (book != null) {
                    return "Title: " + book.getTitle() + ", Author: " + book.getAuthor();
                } else {
                    response.status(404); // 404 Not found
                    return "Book not found";
                }
        });

        // Updates the book resource for the provided id with new information
        // author and title are sent as query parameters e.g. /books/<id>?author=Foo&title=Bar
        put("/books/:id", (request, response) -> {
                String id = request.params(":id");
                Book book = books.get(id);
                if (book != null) {
                    String newAuthor = request.queryParams("author");
                    String newTitle = request.queryParams("title");
                    if (newAuthor != null) {
                        book.setAuthor(newAuthor);
                    }
                    if (newTitle != null) {
                        book.setTitle(newTitle);
                    }
                    return "Book with id '" + id + "' updated";
                } else {
                    response.status(404); // 404 Not found
                    return "Book not found";
                }
        });

        // Deletes the book resource for the provided id 
        delete("/books/:id", (request, response) -> {
                String id = request.params(":id");
                Book book = books.remove(id);
                if (book != null) {
                    return "Book with id '" + id + "' deleted";
                } else {
                    response.status(404); // 404 Not found
                    return "Book not found";
                }
        });

        // Gets all available book resources (id's)
        get("/books", (request, response) -> {
                String ids = "";
                for (String id : books.keySet()) {
                   ids += id + " "; 
                }
                return ids;
        });

    }

}
<p>

更多代码使用案例见Github

该框架的Server底层目前可惜不是基于Netty或Vert.x等异步IO的,可以在Jetty和Tomcat中运行,如果能直接集成Vet.x,就是一个完整的Java版的Node.js+Express.js了。

[该贴被banq于2014-06-07 09:18修改过]

              

1