resteasy

Creating JAX-RS web service using RESTEasy Example

Introduction

RESTEasy is a JBoss Project and an implementation of JAX-RS specification.  It’s simplified nature made a huge noise on the backend developers community and to this day, one of the widely used JAX-Rs implementation. How easy is it? Let’s find out.

What are we doing?

We are doing a step by step guide on how to create a RESTEasy service.
 
 
 

1. Create a webapp using Maven and plug in the RESTEasy dependency.

We need to add the dependency first on your web app.

pom.xml

<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>com.javacodegeeks.areyes</groupId>
	<artifactId>resteasy-sample</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>resteasy-sample Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<repositories>
	   <repository>
		  <id>JBoss repository</id>
		  <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
	   </repository>
	</repositories>
	
	<dependencies>
		<dependency>
			<groupId>org.jboss.resteasy</groupId>
			<artifactId>resteasy-jaxrs</artifactId>
			<version>2.2.1.GA</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>resteasy-sample</finalName>
	</build>
</project>

2. Additional configuration on the webapp

We also need to put additional configurations on the web.xml file as we need the app to recognize our service.

web.xml

	
 <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <!-- Auto scan REST service -->
	<context-param>
		<param-name>resteasy.scan</param-name>
		<param-value>true</param-value>
	</context-param>
 
	<!-- this need same with resteasy servlet url-pattern -->
	<context-param>
		<param-name>resteasy.servlet.mapping.prefix</param-name>
		<param-value>/rservices</param-value>
	</context-param>
 
	<listener>
		<listener-class>
			org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
		</listener-class>
	</listener>
 
	<servlet>
		<servlet-name>resteasy-servlet</servlet-name>
		<servlet-class>
			org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
		</servlet-class>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>resteasy-servlet</servlet-name>
		<url-pattern>/rservices/*</url-pattern>
	</servlet-mapping>
	
</web-app>

3. Create the service

The fun part! Let’s create the service!

CustomerRestService.java

package com.javacodegeeks.areyes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
 
@Path("/customer")
public class CustomerRestService {
 
	@GET
	@Path("/printmessage/{param}")
	public Response printMessage(@PathParam("param") String msg) {
 
		String result = "Restful Return! : " + msg;
 
		return Response.status(200).entity(result).build();
 
	}
 
}

As it can be seen above, there’s a bunch of Annotations that we used. These annotations are crucial as it allows the JVM to categorize this source code to have a configuration injected to it. The following annotations are described below:

  • @Path – The endpoint url path.
  • @Get – This means that the method is called using the GET http method.
  • @PathParam – Query parameters passed via the URL.

What I really liked about RESTEasy is that it lives up to it’s name. It’s just plain simple. Just annotate and your service is ready to go!

4. Let’s test it

Deploy your app to a J2EE container and go to:

http://localhost:8080/resteasy-sample/rservice/customer/printmessage/<your message>

Figure 1: RESTeasy sample output

Download the Eclipse Project

This was an example of creating a JAX-RS compliant web service using RESTEasy

Download
You can download the full source code of this example here: resteasy-sample.zip

Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
sobia aijaz
sobia aijaz
5 years ago

i deployed ur war, and got this error

Message /resteasy-sample/rservice/customer/printmessage/message

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.42

any ideas?

Back to top button