REST With Java 8
See how you can build your own REST services with Java, Maven, and the Spark Java framework.
Join the DZone community and get the full member experience.
Join For FreeLet'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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spark.rest</groupId>
<artifactId>TestRest</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
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”.
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies>
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:
http://localhost:4567/rest
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.
Opinions expressed by DZone contributors are their own.
Comments