|
Using Jersey with Orion This article will show how to get started with RESTful Web-Services on the Orion Application Server, using the reference implementation of JAX-RS (Java API for RESTful Web-Services, JSR 311) which is the open source project Jersey of the Glassfish project.
Introduction
Setting up the environment Writing your first Resource Compiling the Resource Testing the Root Resource Testing the Sub-Resource Summary 1 Introduction 1.1 REST The concept of REST (REpresentational State Transfer) was created by Roy Fielding around year 2000 in his doctoral dissertion , chapter 5 . It is basically an architecture for communication between machines. The idea behind REST is to expose Resources with unique identites in the form of URI:s, just like the World Wide Web. These resources are then accessed by Consumers through standard HTTP methods (such as GET, POST, PUT, OPTIONS, etc), and the Resource then respond with a suitable representation of its content based upon negotiation of mime-types based upon the closest match between what the Consumer says it can handle (through the an applications functions and state are abstracted into a Resource every Resource has a unique address using a universal syntax that can be used in hypermedia links every Resource share a uniform interface through which state can be transfered between the Consumer and the Resource the interface is based upon standard HTTP methods (such as POST, GET, OPTIONS, etc) for the Consumer to use the interface has a set of Representations (based on mime-types) for the Resource to represented as Compared to SOAP, REST is much lighter, said to be faster to implement and is based on far less standards. 1.2 JAX-RS JAX-RS is a Java API for RESTful Web Services. The specification was developed through JSR 311. The JSR homepage is at http://jcp.org/en/jsr/detail?id=311, from where the specification and API docs can be downloaded. 1.3 Jersey Jersey is the open source JAX-RS (JSR 311) Reference Implementation for building RESTful Web services. Read more about Jersey at http://jersey.dev.java.net. 2 Setting up the environment
2.1 Download Orion Before we can get started with our firs steps in JSP programming we need to install the Server. If you have already installed Orion, then please skip to the next section. Now orion is installed and ready to use with the default configuration. This means Orion will install itself using port 80 for the web server part. If you require more information about setting up the Orion Application Server, please read this article. If you look in the installation directory you will notice a directory named default-web-app. This directory holds the default Web-module that is shipped with Orion. When you access the server at http://localhost/ the response is generated by the file named index.html from the default-web-app directory. The examples directory holds a number of example JSP pages and Servlets that you can play around with later and use as samples for further experimenting with dynamic content. We will use the default-web-app directory as home for the REST resources that we will develop throughout this article. 2.2 Download Jersey From the Jersey project home, download the following files: Put all the downloaded jar files in the [path]/orion/default-web-app/WEB-INF/lib directory. Please notice that later versions of Jersey than 1.0 might require other jars to be downloaded. 2.3 Enable Development-mode Edit the file [path]/orion/config/global-web-application.xml so that the <orion-web-app> tag looks something like in listing 1 below.
In the listing above, we set the attribute development to "true" which will tell the Container to run the Web-module in development-mode, which amongst other things allow sfor automatic compilation of Servlets and other classes these might be dependent upon. We will utilize this feature to let the Container compile our REST Resources for us, as will be explained later. 2.4 Enable Jerseys ServletAdaptor We will be using Jerseys ServletAdaptor to hook Jersey into Orion. In your favourite editor, open the file [path]/orion/default-web-app/WEB-INF/web.xml and add the following lines after the
The 2.5 Verify your setup ![]()
Do not let the warnings alarm you. That is just Jersey tellling us that there are currently no Root Resources available. Something we will attend to shortly. 3 Writing your first Resource In the [path]/orion/default-web-app/WEB-INF/classes directory, create a file named HelloWorldResource.java and open it up for editing with your favourite editor. First, lets add the neccessary imports:
Now, continue with the following:
Root Resources are POJOs (Plain Old Java Objects) with atleast one The Continue with the following:
The The ![]()
Lets try and not say anything witty about the method itself as all it does is returning the text "Hello world!" to the consumer.. Instead, lets hurry onwards to the next method that will be just a tad more interesting. Continue with the following:
Again, the Now we got a By using method-level ![]()
4 Compiling the Resource As we have enabled development-mode for our web-module, lets take advantage of that and let Orion compile our Resource for us. ![]()
Look in the [path]/orion/default-web-app/WEB-INF/classes directory. You should see that Orion has generated ![]()
5 Testing the Root Resource ![]()
Congratulations! Your Root Resource is working as expected! If you were a machine in dire need of communicating with other machines, I bet you would be humming happily right now! 6 Testing the Sub-Resource ![]()
Congratulations! Your Sub-Resource is working as expected! You just utilized a Sub-Resource and even provided it with a variable! 7 Summary Throughout this article you have learned how to configure Orion to use Jersey in order to be able to develop and host RESTful applications. You have also tried out writing both Root Resources and Sub-Resources as well as used Orions development-mode to automatically compile these classes for you. You then confirmed that everything worked by acting as a Consumer of your newly created Resources. If you are new to REST, you might want to learn more through some of the REST tutorials out there, or you could try another of the Orion tutorials and introductions available here. Copyright © 2008 IronFlare AB |