Press "Enter" to skip to content

Simple JAX-RS Web Service on JBoss (RESTEasy)

Implementing RESTful services in PHP or Ruby was very easy, I love its simplicity compared to SOAP. But implementing a RESTful service for the first time in Java has caused me a bit of a headache. In the beginning I was quite confused when it came to JAX-RS specification and the different implementations (Jersey, RESTEasy, …), but then it’s so easy, I don’t know why I have wasted so much time figuring out what I needed…

We are using JBoss to deploy our application, which comes packed with RESTEasy, JBoss’s implementation of the JAX-RS, so setting up a project is piece of cake. The pom.xml of my project looks quite simple. The jsr311-api dependency is only needed for the time of development, as it comes with JBoss, so we can set the scope as provided. RESTEasy is also provided by the application server, so there is no need to add it to the dependencies.

<?xml version="1.0" encoding="UTF-8"?>  
<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>net.gazsi.laszlo.sandbox</groupId>
    <artifactId>jboss-resteasy-example</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>1.1.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>sandbox</finalName>
    </build>

</project>  

I have created a very simple sayHello method, which takes a name:String parameter and returns a String:

package net.gazsi.laszlo.sandbox.ws;

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

@Path("hello")
public class RestExample {

    @GET
    @Path("{name}")
    public String sayHello(@PathParam("name") String name){
        StringBuilder stringBuilder = new StringBuilder("SandBox | Hello ");
        stringBuilder.append(name).append("!");

        return stringBuilder.toString();
    }

}

Setting up the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app 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_3_0.xsd" version="3.0">
    <display-name>Laszlo Gazsi - Sandbox</display-name>

    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>  

After packaging into a war file, we are ready to deploy our test project. If we make a GET request to http://localhost:8080/sandbox/rest/hello/Laci we should see the SandBox | Hello Laci! message.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *