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.

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: 

2. Add maven dependencies

Here is the complete maven pom.xml file. It contains dependencies for RESTEasy, Jackson provider, and RESTEasy client.

<?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.favtuts</groupId>
	<artifactId>resteasy-hello-world-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>resteasy-hello-world-example Maven Webapp</name>
	<!-- FIXME change it to the project's website -->
	<url>https://www.tuts.heomi.net</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.7</maven.compiler.source>
		<maven.compiler.target>1.7</maven.compiler.target>
		<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:

resteasy-hello-world-example/
├── pom.xml
├── README.md
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── favtuts
│   │   │           └── resteasy
│   │   │               ├── model
│   │   │               │   └── HelloWorld.java
│   │   │               └── resource
│   │   │                   ├── HelloWorldResource.java
│   │   │                   └── RestEasyServices.java
│   │   ├── resources
│   │   └── webapp
│   │       ├── index.jsp
│   │       └── WEB-INF
│   │           └── web.xml
│   └── test
└── target

4. Create a HelloWorld model class

package com.favtuts.resteasy.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class HelloWorld {
	
	private String message;

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

	public String getMessage() {
		return message;
	}

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

5. Create HelloWorldResource class

Finally, let’s create an actual API definition here:

package com.favtuts.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 com.favtuts.resteasy.model.HelloWorld;


/**
 * Hello World rest api using resteasy
 */
@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 com.favtuts.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() {
		this.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 com.favtuts.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);
	}
}

First run the web project on JBoss EAP 7.4 , then 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.

Download Source Code

$ git clone https://github.com/favtuts/java-jax-rs-tutorials.git
$ cd resteasy-hello-world-example

Run web project on JBoss EAP 7.4

Access: http://localhost:8080/resteasy-hello-world-example/restapi/hello
$ mvn test

References

  1. Maven – How to create a Java web application project
  2. How to Create a Web Project Using Maven in Eclipse
  3. Create a Simple Maven Web Application using Command Line
  4. RESTEasy hello world example

Leave a Reply

Your email address will not be published. Required fields are marked *