Over a million developers have joined DZone.

REST With Java 8

DZone 's Guide to

REST With Java 8

See how you can build your own REST services with Java, Maven, and the Spark Java framework.

· Java Zone ·
Free Resource

Let's develop a REST web service using Java 8 and Spark. It's pretty easy to develop. All you have to do is install Maven and Java 8.

Spark supports REST development in an easy, functional programming style. Please note that the “Spark” framework I mentioned in this article is not related to the Apache Spark Machine Learning framework. It is a lightweight Java web framework that supports for rapid web service development.

You can see a demo video for the below explanation at https://www.youtube.com/watch?v=UbXPyIJk5qI

So, create a directory called “testrest” on your PC. Create a .POM file with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


Please check the groupId, artifactId and version elements of the POM file. You can replace them with your own values. Now we need to add the dependency for “SparkJava”.


The environment and basic structure are ready. Now we need to create a Java file that implements the service. Let’s do that. We can import this project as a “Maven Project” in the Eclipse IDE so that you do not have to create it again. In order to do that, you need run the following command in the “testrest” folder.

  •  mvn eclipse:eclipse

This will make the project “Maven aware” and you can import this project.

Also, you need to create a folder:

  • src\main\java under the “testrest”  folder.

This is the location for our Java source files for developing our REST service.

Now we have our development environment ready.

Create a Java file called “TestRest.java” and add the following code.

import static spark.Spark.*;
public class TestRest {
    public static void main(String[] args) {
       get("/rest", (req, res) -> "Hello Rest”);

Basically, we create a service that will display “Hello Rest” in our browser. The first argument, “get()”, is the REST endpoint, and the second argument is the data that we want to send to the browser.

Access the command prompt and navigate to the “testrest” folder.

Enter the following Maven commands.

  • mvn compile
  • mvn package

The above commands package and create a JAR file with our REST resource, and we need to start the internal server provided by the “Spark Java” framework. For that, enter the following command.

  • mvn exec:java -Dexec.mainClass="TestRest"

The server will start on port number 4567. This is the default port of the Spark Java framework.

Note: You will not get any message saying that the server started on port 4567.

Now open a browser and enter the following URL:


You should see “Hello Rest” in your browser. And that's it! Now you've seen that it's very easy to develop a REST service using Java 8, Maven, and the Spark Java framework.

java 8 ,maven ,rest ,rest api

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}