Rest service Hello World with RestEasy


Tools and Technologies used

HelloWorld_RestEasy

Project structure


RestEasy_hello_world_proj_structure

Step 1

Create a new Maven project with name “RestEasyHelloWold” in eclipse.

Refer Step 1 in this article for the same.

Step 2

Add the RestEasy server dependency to pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4.         http://maven.apache.org/maven-v4_0_0.xsd">
  5.   <modelVersion>4.0.0</modelVersion>
  6.   <groupId>RestEasyHelloWorld</groupId>
  7.   <artifactId>RestEasyHelloWorld</artifactId>
  8.   <packaging>war</packaging>
  9.   <version>0.0.1-SNAPSHOT</version>
  10.   <name>RestEasyHelloWorld Maven Webapp</name>
  11.   <url>http://maven.apache.org</url>
  12.   <dependencies>
  13.        <dependency>
  14.              <groupId>junit</groupId>
  15.              <artifactId>junit</artifactId>
  16.              <version>3.8.1</version>
  17.              <scope>test</scope>
  18.        </dependency>
  19.  
  20.                     <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
  21.        <dependency>
  22.              <groupId>org.jboss.resteasy</groupId>
  23.              <artifactId>resteasy-jaxrs</artifactId>
  24.              <version>3.0.4.Final</version>
  25.        </dependency>
  26.  
  27.     </dependencies>
  28.  
  29.  <build>
  30.     <finalName>RestEasyHelloWorld</finalName>
  31.   </build>
  32.  
  33. </project>
<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>RestEasyHelloWorld</groupId>
  <artifactId>RestEasyHelloWorld</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestEasyHelloWorld Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
       <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>3.8.1</version>
             <scope>test</scope>
       </dependency>

                    <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
       <dependency>
             <groupId>org.jboss.resteasy</groupId>
             <artifactId>resteasy-jaxrs</artifactId>
             <version>3.0.4.Final</version>
       </dependency>

    </dependencies>
 
 <build>
    <finalName>RestEasyHelloWorld</finalName>
  </build>

</project>

We have added the RestEasy dependency in the above pom file.

Step 3

Update web.xml file with RestEasy servlet container

  1. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  2.                          "http://java.sun.com/dtd/web-app_2_3.dtd" >
  3.  
  4. <web-app>
  5.      <display-name>Archetype Created Web Application</display-name>
  6.         <context-param>
  7.               <param-name>resteasy.scan</param-name>
  8.               <param-value>true</param-value>
  9.         </context-param>
  10.    
  11.         <context-param>
  12.           <param-name>resteasy.servlet.mapping.prefix</param-name>
  13.           <param-value>/rest</param-value>
  14.     </context-param>        
  15.    
  16.      <listener>
  17.         <listener-class>
  18.                org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
  19.         </listener-class>
  20.      </listener>
  21.    
  22.     <servlet>
  23.         <servlet-name>Resteasy</servlet-name>
  24.         <servlet-class>
  25.             org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
  26.         </servlet-class>
  27.     </servlet>        
  28.    
  29.     <servlet-mapping>
  30.         <servlet-name>Resteasy</servlet-name>
  31.         <url-pattern>/rest/*</url-pattern>
  32.     </servlet-mapping>
  33.  
  34. </web-app>
<!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>
        <context-param>
              <param-name>resteasy.scan</param-name>
              <param-value>true</param-value>
        </context-param>
   
        <context-param>
	      <param-name>resteasy.servlet.mapping.prefix</param-name>
	      <param-value>/rest</param-value>
	</context-param>         
    
     <listener>
        <listener-class>
               org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        </listener-class>
     </listener>
   
    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
    </servlet>         
   
    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

We have defined a RestEasy servlet with the class name org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
which will act as a Resteasy container.

ResteasyBootstrap is used to scan the Rest classes annotated with JAX-RS annotations.

It uses the values from the context-param elements.

resteasy.servlet.mapping.prefix : In this parameter we define the URI prefix for our Rest Service.

This should be same as servlet-mapping value.

This value should be prefixed with URL to invoke the RestEasy resources.

resteasy.scan : If this value is set to true,ResteasyBootstrap will automatically search for REST Services Implementation classes annotated with JAX-RS annotations (@Path, @GET, @POST etc…) and register them as a Rest service classes.

Step 4

Create a Hello world Rest service

  1. package com.kb.rest;
  2.  
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7.  
  8. @Path("/hello")
  9. public class HelloWorldRestService {
  10.  
  11.     @GET
  12.     @Produces(MediaType.TEXT_PLAIN)
  13.     public String getMessage(){
  14.         return "RestEasy Hello World";
  15.        
  16.     }
  17. }
package com.kb.rest;

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

@Path("/hello")
public class HelloWorldRestService {

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String getMessage(){
		return "RestEasy Hello World";
		
	}
}

@Path – to specify the root path of the resource which is “/hello”

@GET – It’s the HTTP method that is supported by the service

@Produces – It’s used to specify the MIME type that service is supporting which is Plain Text here.

Step 5

Build the project and deploy the project in the Tomcat server

Step 6

Start the server

Step 7

Access the Rest service using below URL

http://localhost:8080/RestEasyHelloWorld/rest/hello

RestEasy_hello_world_output

Download this project RestEasyHelloWorld1.zip


Alternative way of Implementing Rest service using RestEasy


We can also achieve Rest service implementation by defining our application class by extending javax.ws.rs.core.Application.

In this case, we don’t need to provide resteasy.scan to scan the Rest services rather we just need to make an entry of our Application class in the web.xml

MyRestEasyApplication.java

  1. package com.kb.rest;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Set;
  5.  
  6. import javax.ws.rs.core.Application;
  7.  
  8. public class MyRestEasyApplication extends Application {
  9.  
  10.     private Set<Object> singletons = new HashSet<Object>();
  11.  
  12.     public MyRestEasyApplication() {
  13.         singletons.add(new HelloWorldRestService());
  14.     }
  15.  
  16.     @Override
  17.     public Set<Object> getSingletons() {
  18.         return singletons;
  19.     }
  20.  
  21. }
package com.kb.rest;

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

import javax.ws.rs.core.Application;

public class MyRestEasyApplication extends Application {

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

	public MyRestEasyApplication() {
		singletons.add(new HelloWorldRestService());
	}

	@Override
	public Set<Object> getSingletons() {
		return singletons;
	}

}

In this class, we just need to add all our Rest service classes to singletons HashSet as shown above.

This will be used by the RestEasy framework to identify the appropriate Resource based on request URL.

Web.xml

  1. <!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  2.                           "http://java.sun.com/dtd/web-app_2_3.dtd" >
  3.  
  4. <web-app>
  5.   <display-name>Archetype Created Web Application</display-name>
  6.   <!--  <context-param>
  7.         <param-name>resteasy.scan</param-name>
  8.         <param-value>true</param-value>
  9.     </context-param> -->
  10.     <context-param>
  11.         <param-name>resteasy.servlet.mapping.prefix</param-name>
  12.         <param-value>/rest</param-value>
  13.     </context-param>        
  14.     <listener>
  15.         <listener-class>
  16.        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
  17.         </listener-class>
  18.     </listener>
  19.     <servlet>
  20.         <servlet-name>Resteasy</servlet-name>
  21.         <servlet-class>
  22.             org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
  23.         </servlet-class>
  24.         <init-param>
  25.       <param-name>javax.ws.rs.Application</param-name>
  26.       <param-value>com.kb.rest.MyRestEasyApplication</param-value>
  27.     </init-param>
  28.     </servlet>        
  29.     <servlet-mapping>
  30.         <servlet-name>Resteasy</servlet-name>
  31.         <url-pattern>/rest/*</url-pattern>
  32.     </servlet-mapping>
  33. </web-app>
<!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>
  <!--  <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
    </context-param> -->
    <context-param>
		<param-name>resteasy.servlet.mapping.prefix</param-name>
		<param-value>/rest</param-value>
	</context-param>         
    <listener>
        <listener-class>
       org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
        <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>com.kb.rest.MyRestEasyApplication</param-value>
    </init-param>
    </servlet>         
    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

We have removed the resteasy.scan context parameter to scan the Resources but we have included our application class in the init-param as below

  1. <init-param>
  2.       <param-name>javax.ws.rs.Application</param-name>
  3.       <param-value>com.kb.rest.MyRestEasyApplication</param-value>
  4. </init-param>
<init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>com.kb.rest.MyRestEasyApplication</param-value>
</init-param>

This will also yield the same output.

Download this project RestEasyHelloWorld2.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on