Home » Spark Java

Spark Java

What is Spark Java?

Spark is a Java micro framework that allows to quickly create web applications in Java 8. Spark is a lightweight and simple Java web framework designed for quick development. Sinatra, a popular Ruby micro framework, was the inspiration for it.

Spark makes considerable use of Java 8’s lambda expressions, that makes Spark applications less verbose. Unlike other Java web frameworks, it does not largely rely on XML files or annotations.

Starting with Spark Java

Step 1: Create a maven project and include the dependency in the pom.xml

Step 2: Copy the following code.

Step 2: Enter the following URL in the web browser to see the results.

Starting the Server

When we perform something that requires the server to be started, it is automatically started (i.e. declaring a route or setting the port).

We can also start the server manually by typing init ().

If initialization fails, we can describe what should happen:

Logging and shutting down is the default behaviour:

Stopping the Server

The server is stopped and all routes are cleaned when the stop() method is invoked.

Routes

There are a number of routes in a Spark application. A route is a mapping between URL patterns and Java handlers.

There are three parts to a route:

  1. A verb – get, post, put, delete, head, trace, connect, and options
  2. a path – like /first or /hello/:name
  3. a callback – (request, response) ->{ }

The order in which routes are defined determines how they are matched. The request is forwarded to the first route that matches the request.

To maintain decent readability, always statically import Spark methods:

The params() method on the request object can be used to retrieve named parameters in route patterns:

Splat (or wildcard) parameters can also be used in route patterns. The splat() method on the request object can be used to retrieve these parameters:

Unmapping of routes

The ‘unmap’ function can be used to unmap routes:

Groups of paths

Separating routes into groups can be useful if you have a lot of them. It is accomplished by invoking the path() method. The method accepts a String prefix and returns a scope in which to create routes and filters (or nested paths):

Request

The request parameter provides the following information and functionality:

Response

The response parameter provides the following information and functionality:

Query Maps

We can use query maps to group arguments into a map based on their prefix. It allows us to add two parameters to a user map, such as user[name] and user[age].

Cookies

Sessions

Every request has access to the server-side session, which is given through the following methods:

Halting

Use halt() method to immediately stop a request within a filter or route:

Note: halt() should not be used inside an exception-mapper.

Filters

Before-filters are examined before each request and can read and alter the request and response.

Use halt() method to halt the program’s execution:

Following each request, after-filters are examined, and they can read the request and read/modify the response:

Following after-filters, after-after-filters are examined. Consider it a “finally” block.

Filters can optionally take a pattern, it means they’ll only be evaluated if the request path matches it:

Redirects

With the redirect method on the response, we can force a browser redirect:

A browser redirect can also be triggered by a specific HTTP 3XX status code:

API for redirection

There is also a redirection convenience API that can be used without the response:


Next TopicLock in Java

You may also like