DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > REST With Java 8

REST With Java 8

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

Unni Mana user avatar by
Unni Mana
CORE ·
Nov. 27, 16 · Java Zone · Tutorial
Like (53)
Save
Tweet
49.99K Views

Join the DZone community and get the full member experience.

Join For Free

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"
         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.


REST Web Protocols Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 9 Strategies to Improve Your Software Development Process
  • SQL vs. NoSQL: Pros and Cons
  • Machine Learning in Cybersecurity
  • The Importance of Semantics for Data Lakehouses

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo