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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Designing for Sustainability: The Rise of Green Software
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Designing AI Multi-Agent Systems in Java
  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 user avatar
Unni Mana
·
Updated Nov. 27, 16 · Tutorial
Likes (53)
Comment
Save
Tweet
Share
51.6K 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.

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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!