Restful web services using RESTEasy hello world example.
In this page we are giving simple hello world restful web service example using RESTEasy framework. RESTEasy is JBOSS provided
implementation of JAX-RS specification to build java based restful web service.
Here is the directory structure:

We need resteasy related jar files, here is the pom.xml file:
<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>RestfulWebServices</groupId>
<artifactId>RestfulWebServices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.7.Final</version>
</dependency>
</dependencies>
</project>
|
Simple service class with @Path annotations, you will get more details about these annotations going ahead in this site.
package com.java2novice.restful;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/publish")
public class RestEasyExample {
@GET
@Path("/{message}")
public Response publishMessage(@PathParam("message") String msgStr){
String responseStr = "Received message: "+msgStr;
return Response.status(200).entity(responseStr).build();
}
}
|
We need to configure resteasy within our web.xml file. We should set “resteasy.scan” to true, so that RESTEasy will find
and register your REST service by default. Here is the web.xml file for your reference:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</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>/*</url-pattern>
</servlet-mapping>
</web-app>
|
Now create war file and deploy it either in tomcat or jboss. Run the server and open link http://localhost:8080/RestfulWebServices/publish/{user-input}
in the browser. Here {user-input} value can be dynamic, this value will be received by the rest service and returns as a response:

|