DZone
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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above
  • Aggregating REST APIs Calls Using Apache Camel
  • Choosing a Library to Build a REST API in Java

Trending

  • AI Coding Agent Guardrails: Finding Gaps in Tool Permissions
  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • Genkit Flows as senro Steps: Extremely Powerful AI Pipelines in Go
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. 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.

By 
Unni Mana
·
Updated Nov. 27, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
52.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above
  • Aggregating REST APIs Calls Using Apache Camel
  • Choosing a Library to Build a REST API in Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook