Tutorial: Getting started with RESTEasy (a Java REST Framework) and Netbeans 7.1

Post to Twitter

RESTEasy is quickly becoming a favorite project of mine. RESTEasy allows you to easily and quickly create REST based services with Java. Today I’ll show you how fast you can have a minimal REST service running using Netbeans 7.1 and RESTEasy.


Create a new Maven Web Application using Netbeans 7.1 and call it HelloRESTEasy.

By default an index.jsp page is created, go ahead and remove it as we won’t need it.

Now we will add the RESTEasy dependencies to the project. With Netbeans this is simple. Right-click on Dependencies and select: Add Dependencies…

In the dialog that pops up enter resteasy-jaxrs into the query textbox.

My Maven pom.xml file ended up looking like this with the dependency added:


    4.0.0

    com.giantflyingsaucer
    HelloRESTEasy
    1.0-SNAPSHOT
    war

    HelloRESTEasy

    
        ${project.build.directory}/endorsed
        UTF-8
    

    
        
            org.jboss.resteasy
            resteasy-jaxrs
            2.3.2.Final
        
        
            javax
            javaee-web-api
            6.0
            provided
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    1.6
                    1.6
                    
                        ${endorsed.dir}
                    
                
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.1.1
                
                    false
                
            
            
                org.apache.maven.plugins
                maven-dependency-plugin
                2.1
                
                    
                        validate
                        
                            copy
                        
                        
                            ${endorsed.dir}
                            true
                            
                                
                                    javax
                                    javaee-endorsed-api
                                    6.0
                                    jar
                                
                            
                        
                    
                
            
        
    


Add a new class to the default package called: HelloService

The code for our REST service is simple. What it will do is allow us to do an HTTP GET and return a greeting. We can pass in a name and have that sent back for the greeting. Here is the code:

package com.giantflyingsaucer.helloresteasy;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;


@Path("/")
public class HelloService {

    @GET
    @Path("/Hello/{name}")
    public Response printMessage(@PathParam("name") String name) {
        return Response.status(200).entity("Hello " + name).build();
    }
}

The final step before you compile the project is to add a web.xml file with the following contents:






    
        resteasy.scan
        true
    
    
        resteasy.servlet.mapping.prefix
        /
    
    
        HelloService
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    
    
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    
    
        HelloService
        /*
    

Build the project. Now you can take the resulting WAR file and drop it into Apache Tomcat’s webapps folder. Start Tomcat and test out the following link (make corrections to the path you setup):

http://localhost:8080/HelloRESTEasy-1.0-SNAPSHOT/Hello/Chad

Expected results:

Hello Chad

Post to Twitter

This entry was posted in Apache Tomcat, Java, REST. Bookmark the permalink.