天天看點

Spark Getting startedGetting startedStopping the ServerRoutesRequestResponseQuery MapsCookiesSessionsHaltingFiltersRedirectsException MappingStatic FilesResponseTransformerViews and TemplatesPortEmbedded webserverOther webserverJavadocExamples

<a target="_blank" href="http://sparkjava.com/documentation.html#getting-started">Getting Started</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#stopping-the-server">Stopping the server</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#routes">Routes</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#request">Request</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#response">Response</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#query-maps">Query Maps</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#cookies">Cookies</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#sessions">Sessions</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#halting">Halting</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#filters">Filters</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#redirects">Redirects</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#exception-mapping">Exception Mapping</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#static-files">Static files</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#response-transformer">ResponseTransformer</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#views-templates">Views and Templates</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#freemarker">- Freemarker</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#velocity">- Velocity</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#mustache">- Mustache</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#port">Port</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#embedded-webserver">Embedded Webserver</a>

<a target="_blank" href="http://sparkjava.com/documentation.html#other-webserver">Other Webserver</a>

Add the maven dependency:

Start coding:

Run and view:

That was easy, right? Spark is the simplest Java web framework to set up, while still providing enough functionality for many types of projects.

By calling the stop() method the server is stopped and all routes are cleared.

The main building block of a Spark application is a set of routes. A route is made up of three simple pieces:

A verb (get, post, put, delete, head, trace, connect, options)

A path (/hello, /users/:name)

A callback (request, response) -&gt; { }

Routes are matched in the order they are defined. The first route that matches the request is invoked.

Route patterns can include named parameters, accessible via the params method on the request object:

Route patterns can also include splat (or wildcard) parameters. These parameters can be accessed by using the splat method on the request object:

In the handle method request information and functionality is provided by the request parameter:

In the handle method response information and functionality is provided by the response parameter:

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.

Every request has access to the session created on the server side, provided with the following methods:

To immediately stop a request within a filter or route use:

You can also specify the status when halting:

Or the body:

...or both:

Before filters are evaluated before each request and can read the request and read/modify the response. 

To stop execution, use halt:

After filters are evaluated after each request and can read the request and read/modify the response:

Filters optionally take a pattern, causing them to be evaluated only if the request path matches that pattern:

You can trigger a browser redirect with the redirect helper method:

You can also trigger a browser redirect with specific http 3XX status code:

To handle exceptions of a configured type for all routes and filters:

You can assign a folder in the classpath serving static files with the staticFileLocation method. Note that the public directory name is not included in the URL.

A file /public/css/style.css is made available as http://{host}:{port}/css/style.css

You can also assign an external folder (not in the classpath) serving static files with the externalStaticFileLocation method.

Mapped routes that transforms the output from the handle method. This is done by extending the ResponseTransformer and pass this to the mapping method. Example Of a route transforming output to JSON using Gson:

and how it is used (MyMessage is a bean with one member 'message'):

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.

Renders objects to HTML using the Freemarker template engine.

Maven dependency:

Renders objects to HTML using the Velocity template engine.

Renders objects to HTML using the Mustache template engine.

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

Standalone Spark runs on an embedded Jetty web server.

To run Spark on a web server instead of standalone first of all an implementation of the interface spark.servlet.SparkApplication is needed. In the init() method the routes should be initialized. In your web.xml the following filter needs to be configured:

The result is put in /target/site/apidocs