• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RESTFul web service

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please help me how to implement web service using Restlet that will do summation of two integers provided by the client?Thanks
 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
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.





To invoke the service, past the following URL in your browser: http://localhost:4000/addition?param1=3¶m2=4
Best wishes!
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response. But how to deal if parameters are in xml request do I have touse parser inside the code? How ?
Why do you use Grizzly? Thank you again.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the advantages of REST is that the requests are generally not XML - all the information is part of the URL.

When you say "parameters are in xml", do you mean a SOAP web service?
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
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.
Best wishes!
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again!
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.

Best wishes!
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Good luck
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
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!
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :










And when executing it using the following http://localhoat:8182?param1=2&PARAM2=6
I got 404 error. Could you please tell me what is the reason?Thanks in advance.
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
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.
Best wishes!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic