Thursday, October 3, 2013

REST Service on JBoss 7 (using RESTEasy) - Part 1

In this section we are going to see how we can host a simple REST service on JBoss AS 7.2

1. Start NetBeans 7.3.1 and create a new maven-web project as follows.


2. Name it as JbossTest.



3.  Choose JBoss as the server.


4. Click Finish

5. You will see the following under Projects tab



6. Create a class named TestService and copy paste the content from below.

package org.jbosstest.jbosstest;

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

@LocalBean
@Path("/test")
public class TestService {

    @GET
    @Path("hello/{name}")
    public String sayHello(@PathParam("name") String name) {
        return "Hello " + name;
    }
}

7. Create a class called ApplicationConfig and paste the following content. This is to register the class TestService as a RESTService.


package org.jbosstest.jbosstest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("webresources")
public class ApplicationConfig extends Application {
}


Hit the browser with http://localhost:8080/JbossTest/webresources/test/hello/world

You will see the following screen.