RESTEasy Hello World Example Tutorial

RESTEasy hello world example tutorial shows how to create simple hello world rest web service with RESTEasy.
Before getting started, I would like to give you an overview of JAX-RS Java API.

Video

The same tutorial explained in below YouTube video:

What is JAX-RS?

JAX-RS (Java API for RESTful Web Services) is a set of Java API that provides support in creating REST APIs. And the framework makes good use of JAX-RS annotations to simplify the development and deployment of these APIs.
JAX-RS 2.0 (JSR-339) and JAX-RS 2.1 (JSR-370), are JCP (Java Community Process) specifications that provide a Java API for RESTful Web Services over the HTTP protocol.
Some of the more well known JAX-RS API implementations are RESTEasy and Jersey.

What is RESTEasy?

RESTEasy is a Java framework for developing RESTful Web Services. It is a fully certified and portable implementation of the JAX-RS 2.0 specification.
RESTEasy provides tighter integration with the JBoss Application Server but we can deploy it on any servlet container like Tomcat, Jetty, etc.
In this tutorial, we use the tomcat server to deploy the RESTEasy web application.

Tools and Technologies used

  • JDK 1.8 or later
  • Maven 3.5+
  • Eclipse IDE
  • JAX-RS 2.0 +
  • RESTEasy - 3.9.3.Final
  • Tomcat 8.5+

Development Steps

  1. Create a Maven Web project in Eclipse IDE
  2. Add maven dependencies
  3. Project Structure
  4. Create a HelloWorld model class
  5. Create HelloWorldResource class
  6. Create Application Class
  7. RESTEasy Client for REST API

1. Create a Maven Web project in Eclipse IDE

Refer below guide to create a web project in eclipse IDE: 
https://www.javaguides.net/2018/11/how-to-create-web-project-using-maven-in-eclipse.html

2. Add maven dependencies

Here is the complete maven pom.xml file. It contains dependencies for RESTEasy, Jackson provider, and RESTEasy client.
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.javaguides</groupId>
    <artifactId>resteasy-hello-world-example</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>resteasy-hello-world-example Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <resteasy.version>3.9.3.Final</resteasy.version>
    </properties>
    <dependencies>
        <!-- Set up RESTEasy -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <!--  RESTEasy Client Dependency -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>resteasy-hello-world-example</finalName>
    </build>
</project>

3. Project Structure

Refer below screenshot for project structure and packaging structure:

4. Create a HelloWorld model class

package net.javaguides.resteasy.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class HelloWorld {
    private String message;

    public HelloWorld() {}

    public HelloWorld(String message) {
        super();
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

4. Create HelloWorldResource class

Finally, let's create an actual API definition here:
package net.javaguides.resteasy.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import net.javaguides.resteasy.model.HelloWorld;

/**
 * Hello World rest api using resteasy
 * 
 * @author Ramesh Fadatare
 *
 */

@Path("hello")
public class HelloWorldResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response helloWorld() {

        HelloWorld helloWorld = new HelloWorld("Hello World !");

        return Response.ok(helloWorld).build();
    }
}
Let's understand the JAX-RS annotations from the above code:
  • The @Path annotation specifies the URL to which the resource responds.
  • The @GET annotation indicates that the annotated method responds to HTTP GET requests.
  • With the @Produces annotation, we define that the method produces JSON.

6. Create Application Class

Let's create an application configuration class. The Application defines the components of a JAX-RS application and supplies additional meta-data. The javax.ws.rs.core.Application class is a standard JAX-RS class that you may implement to provide information on your deployment:
package net.javaguides.resteasy.resource;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/restapi")
public class RestEasyServices extends Application {

    private Set < Object > singletons = new HashSet < Object > ();

    public RestEasyServices() {
        singletons.add(new HelloWorldResource());
    }

    @Override
    public Set < Object > getSingletons() {
        return singletons;
    }
}
Notice that with the @ApplicationPath annotation, we set the path to RESTful web services.

7. RESTEasy Client for REST API

Let's create a JUnit test case with RESTEasy client to test hello world rest API:
package net.javaguides.resteasy;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.junit.Test;

public class HelloWorldResourceTest {

    private static final String FULL_PATH = "http://localhost:8080/resteasy-hello-world-example/restapi/hello";

    @Test
    public void testHelloWorld() {

        final ResteasyClient client = new ResteasyClientBuilder().build();
        final ResteasyWebTarget target = client.target(FULL_PATH);
        String response = target.request().get(String.class);
        System.out.println(response);
    }
}
Run above the JUnit test case to test the hello world REST API. Here is the output:

Conclusion

In this quick tutorial, we introduced RESTEasy and we built a super simple hello world rest API with it.

Comments