File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes Rest based post for receiving and reponding with json Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Rest based post for receiving and reponding with json" Watch "Rest based post for receiving and reponding with json" New topic
Author

Rest based post for receiving and reponding with json

Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
First, what i wanted to know is what i am doing is the right way to do it.

I have a scenario where i have will receive a json request and i have to update the database with that, once the db is updated i have to respond back with the json acknowledgment.

What i have done so far is create the class extending application as follows:

My resource class is extending the ServerResource and i have the following method in my resource class


My first concern is the object i receive in the entity is inputrepresentation so when i fetch the jsonobject from the jsonrepresentation created i always get empty/null object.

I have tried passing the json request with the following code as well as the client attached


Client used to call


Any help or clues on this is hight appreciated ?

Thanks,
Rahul


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
If you are using the RESTlet framework to develop your service, you may want to have a look at the org.restlet.Restlet class as a superclass for your resource class. Using the ServerResource class will cause an instance of the resource class to be created for each request, while using the Restlet class will cause one instance to handle all requests. Perhaps this is what you want - then all is good and well.
If you use the Restlet class as a superclass, then you will implement a handle method along the following lines (just an example to show how to access data of a request sent to the resource):

Best wishes!
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Ivan,

Appreciate your reply on this. Not sure how does this solves the above mentioned problem but it is a nice suggestion in terms of best practices.
I tried doing it the way you suggested but i continue getting an server error.

My Test Client Code is :


The code on the server is as follows( It does not have any annotations as you suggested


Highly appreciate any clues or pointers in solving this.

Thanks,
Rahul
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Seems like this needs to be done step-by-step...
How about if you add a print statement that logs the payload of the HTTP request, i.e. the contents of the theRequestPayloadString variable?
What do you see then?
Best wishes!
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Ivan,

the resource method is not even called when i tried debugging with eclipse, it seems server is somehow not able to find the matching resource. but when i do the other way which ie by extending ServerResource, it calls and had the problem discussed before.

Any suggestion are appreciated.

Thanks,
Rahul
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Well, if you use your original approach, what is the output if you print the payload received by the service?

If you want to see the approach I have used when programming a RESTlet service, please take a look at this post:
http://www.coderanch.com/t/453601/Web-Services/java/RESTFul-web-service
Best wishes!
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Ivan,

Thanks for the link, I will try that and let you know how it goes.

Thanks,
Rahul
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Rest based post for receiving and reponding with json
 
Similar Threads
How to call struts application through JSON
Error parsing JSON request string
Null values being returned from servlet
RESTFul web service
retrieve JSON from servlet