I've been in the world of software development for 14+ years working with most mainstream programming languages at some point. Chad is a DZone MVB and is not an employee of DZone and has posted 38 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Started With RESTEasy and NetBeans IDE 7.1

05.19.2012
| 7779 views |
  • submit to reddit

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 IDE 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 IDE 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:

<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.giantflyingsaucer</groupId>
    <artifactId>HelloRESTEasy</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
 
    <name>HelloRESTEasy</name>
 
    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.3.2.Final</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
 
</project>

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:

<?xml version="1.0" encoding="UTF-8"?>
 
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
 
    <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>/</param-value>
    </context-param>
    <servlet>
        <servlet-name>HelloService</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>
    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>
    <servlet-mapping>
        <servlet-name>HelloService</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

 

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

 

 

 

Published at DZone with permission of Chad Lung, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Herry Johnson replied on Tue, 2012/06/12 - 2:13pm

What is the use of the method Thread.currentThread().getContextClassLoader().. I am not able to find any proper information to know the significance of this method. Can anyone please suggest me any links that explain or if anyone can explain it, that would be helpful to me...

Quentin Badoud replied on Wed, 2013/05/22 - 9:55am

I can't see the images... "permission denied"

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.