• Entries (RSS)
  • Comments (RSS)

Creating a Hello World RESTful web service with RAD and WebSphere Application Server using IBM JAX-RS

Posted by | Posted in WebSphere | Posted on 22-05-2011

Tagged Under : , , ,

Creating a Hello World RESTful web service with RAD and WebSphere Application Server using IBM JAX-RS

REST is the buzzword now. So its time to learn how to create a RESTful web service now. Here is my simple hello world tutorial on creating a RESTful web service using Rapid Application Developer and IBM JAX-RS. JAX-RS is a Java API for developing REST applications. JAX-RS 1.0 defines a server-side component API to build REST applications. This ia an implementation of the JAX-RS (JSR 311) specification.

All the REST implementations I have seen need a minimum Java 1.5 to run. All these implementations use annotations. So my code will not work on WebSphere Application Server 6 and below versions. The minimum required version is WebSphere Application Server 6.1. I have tested these examples only in WAS 7. You need to have Feature pack for Web 2.0 for WebSphere installed on your application server. So once you have your app server ready with Web 2.0 feature pack installed then you have pretty much everything to get started. Follow the below steps to create your first hello world RESTful web service with RAD and WAS 7. For my development purposes I was using RAD 7.5. But I am sure that the following steps will work even for other RAD version too.

1. Create a Dynamic Web Project. Go to File -> New -> Other -> Web -> Dynamic Web Project. The ‘New Dynamic Web Project’ wizard opens up. Enter a name for your project and fill all the required data. The project name I used was RESTWeb.

2. We need to copy the IBM JAX-RS libraries needs to be copied to your project’s classpath.
Go to APPSERVER_ROOT/web2fep/optionalLibraries/jaxrs_1.X/ directory, and copy the following libraries to WEB-INF\lib folder of your project.

* jsr311-api.jar
* commons-lang.jar
* slf4j-api.jar
* slf4j-jdk14.jar
* ibm-wink-jaxrs.jar

If you are using a WAS 6.1 installation that does not have JAXB libraries installed, you must copy the following JAXB JAR file also to your WEB-INF\lib directory.

* ibm-jaxb-2.1.0.jar

3. Creation of the resource.
3.1. Create a Java class. I called my Java class as HelloWorld.java

	package com.albees.rest;
 
	public class HelloWorld {
 
	}

3.2 Annotate the java class to represent the relative path of the resource.

	package com.albees.rest;
 
	import javax.ws.rs.Path;
 
	@Path("/helloworld")
	public class HelloWorld {
 
	}

Here /helloworld will be the relative path to access this service.

3.3. Create the java method and annotate with GET annotation.

	package com.albees.rest;
 
	import javax.ws.rs.GET;
	import javax.ws.rs.Path;
 
	@Path("/helloworld")
	public class HelloWorld {
 
		@GET
		public String sayHello(){
			return "Hello World";
		}
	}

Whenever an HTTP GET request is received by this class, the sayHello method will be invoked as it is annotated with GET.

OK. Now we are done with the resource creation. We have a class that can be accessed with relative URL /helloworld which will return “Hello World” whenever we send an HTTP GET request.

4. Create an application configuration sub class. This class is used for returning the java JAX-RS java classes. The complete source code for the application configuration sub class is given below.

	package com.albees.rest;
 
	import java.util.HashSet;
	import java.util.Set;
 
	import javax.ws.rs.core.Application;
 
	public class HelloWorldConfig extends Application {
		public Set<Class<?>> getClasses() {
			Set<Class<?>> classes = new HashSet<Class<?>>();
			classes.add(HelloWorld.class);
			return classes;
		}
 
	}

5. Modify the web.xml.
We need to modify the web.xml so that the servlet container knows that the web application is JAX-RS supported and what are the JAX-RS classes by specifying the application configuration sub class as a parameter. My web.xml code is given below.

	<?xml version="1.0" encoding="UTF-8"?>
	<web-app id="WebApp_ID" version="2.5" 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_2_5.xsd">
		<display-name>RESTWeb</display-name>
		<welcome-file-list>
			<welcome-file>index.html</welcome-file>
			<welcome-file>index.htm</welcome-file>
			<welcome-file>index.jsp</welcome-file>
			<welcome-file>default.html</welcome-file>
			<welcome-file>default.htm</welcome-file>
			<welcome-file>default.jsp</welcome-file>
		</welcome-file-list>
 
		<servlet>
			<servlet-name>REST</servlet-name>
			<servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
			<init-param>
				<param-name>javax.ws.rs.Application</param-name>
				<param-value>com.albees.rest.HelloWorldConfig</param-value>
			</init-param>
			<load-on-startup>1</load-on-startup>
		</servlet>
		<servlet-mapping>
			<servlet-name>REST</servlet-name>
			<url-pattern>/rest/*</url-pattern>
		</servlet-mapping>
	</web-app>

We are done with our first JAX-RS RESTful Hello World web service with RAD and WebSphere Application Server. Now it is time to see our RESTful web service in action. Deploy the project in the server and access the service using the following URL.

	http://localhost:9080/RESTWeb/rest/helloworld

Where localhost is the server name and 9080 is your server’s default http port. RESTWeb is your context root and rest is the url pattern for accessing your rest web services. helloworld is the relative path of the resource.

Share

Read More

Comments

13 comments posted onCreating a Hello World RESTful web service with RAD and WebSphere Application Server using IBM JAX-RS

  1. This was very good starting point :)

  2. I have followed this tutorial but I am getting some error.
    My environment information is as fellows
    RAD version: 8.0.2
    WAS version: 7.0

    I have included the following jars in my project
    jsr311-api.jar
    commons-lang.jar
    slf4j-api.jar
    slf4j-jdk14.jar
    ibm-wink-jaxrs.jar

    Firefix, gives the following error
    Error 404: javax.servlet.UnavailableException: SRVE0200E: Servlet [com.ibm.websphere.jaxrs.server.IBMRestServlet]: Could not find required class – class java.lang.ClassNotFoundException: com.ibm.websphere.jaxrs.server.IBMRestServlet

    In IBM RAD’s console I got the following error.
    E com.ibm.ws.webcontainer.servlet.ServletWrapper run [Servlet Error]-[class java.lang.ClassNotFoundException: com.ibm.websphere.jaxrs.server.IBMRestServlet]: java.lang.ClassNotFoundException: class java.lang.ClassNotFoundException: com.ibm.websphere.jaxrs.server.IBMRestServlet

    Any suggestions to fix it?
    Thanks

  3. The required jar file is not there in your classpath. From RAD can you check, are you able to see the IBMServlet class? Is the jar file containing IBMRestServlet class exist in your WEB-INF directory?

  4. Ya it was jar problem.

  5. Hi… How to return a JSON object??

  6. Hi… How to return a JSON object??

  7. Short and to the point. Run first time. Very good starting point as someone said

  8. It was very helpfull. Thanks.

  9. Where could I download the ibm-wink-jaxrs.jar? What will be a substitute jar if this jar is deprecated?

  10. Thanks…Re’ally helpfull.

  11. Hi I can not find ibm-wink-jaxrs.jar and ibm-jaxb-2.1.0.jar, please let me know from where I can get these JAR.

  12. Download and install the Web 2 fixpack for webspehere application server , there you can find the required jar’s.

  13. Short & Crisp to the point.. Thanks!! it was helpful!!

Post a Comment