The following example is a very quick and dirty example on how to implement a standalone RESTful service using the RESTlet framework. I have put the two parameters in the query string, but if you do serious service development you will probably want to enclose a JSON or XML fragment containing the parameters when invoking the service. The Grizzly libraries included are not necessary, but improves the service's capability to cope under load.
As far as I understand doing REST with XML or JSON is fine.
The URL is usually used as a resource identifier and there may be additional parameters, but they are usually used to, for instance, limit the number of results in the response (if it is a list), limit the depth of the representation etc.
Example: http://acme.com/Customers/1123 This URL identifies the Customer with id 1123.
The representation of the resource is enclosed as a payload of the request; for instance if I am creating a new Customer, I use a POST request with the representation of the new Customer in XML or JSON. If I am retrieving a Customer, I use a request to the URL identifying the Customer and obtain a representation in XML or JSON as response.
As said before, I use Grizzly in order to make the RESTlets server cope better under load; with a high frequency of requests, there tend to be occasional losses if Grizzly is not used. Using Grizzly, these occasional losses disappear.
I'll see if I have time to show how to process payloads of request but cannot promise anything.
Here is a piece of sample code showing how to retrieve the HTTP method, the media type and the payload of a request to a RESTlet.
Note that the payload is a raw string and any parsing must be implemented. However, if you have XML payloads and use JAXB this can be quite simple.
First Iwould like to thank you for the help.
Second How to implement the beans ?(I am new to beans and Spring and Grizzly)
Third Can this web service consumed and produced by mobile device?
Finally What is the easiest and fastest way to learn developing Restlet web services practically and notTheoretically?
Thank you very much.
First of all, you do not have to implement anything, or change any settings, to use Grizzly. Just put the JAR files in your classpath.
By the way, to make sure you understood things: Grizzly is the NIO and web framework that is used in GlassFish. Take a look at this webpage for more information: https://grizzly.dev.java.net/ Second, if you want to use JAXB to help you marshall and unmarshal XML payload, you just write an XML schema describing the payload and compile it with the JAXB schema compiler. The output will be annotated Java classes. For more information on JAXB, I can recommend for instance chapter 17 in the JavaEE 5 Tutorial, which you can find here: http://java.sun.com/javaee/5/docs/tutorial/doc/ If you want to use JSON payloads, then you have to write the classes representing the payload yourself. You may want to take a look at Jackson, which is a JSON processor: http://jackson.codehaus.org/ Best wishes!
Since I didnot understand the beans and Spring I did the following, created main class to create server connector on localhost on port 8182 and one application class and the other is the resource class :
In case request is sent as an XML how the request will look like ? shall the parameters will be passed in the get command and if there is a post command will it post the parameters at once or one per request.Thanks. I want more clarifying please
If you are using Apache HttpClient, you can create a new StringRequestEntity with the XML or the JSON and attach it to the request.
Typically for a RESTful service, the payload is a representation of the resource being manipulated. I cannot tell you what representations of your resources will look like, since I have no idea what kind of resources you have.