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 sending java Object to REST web services. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "sending java Object to REST web services." Watch "sending java Object to REST web services." New topic
Author

sending java Object to REST web services.

swapnil kachave
Greenhorn

Joined: Feb 10, 2010
Posts: 27
Hi,

I created rest web services.
I can pass String ,int to REST web services.

But when i go for passing custom object to services it throw "500 Exception".

I am not getting what is exact error.

can any on help me.

Thanks in Advance
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
One of the main points of web services is their cross-platform compatibility. While I'm not quite sure what you mean by "custom object", and you don't mention how you're trying to send that, is sounds like a serialized Java object, which throws away this major advantage.
Jim Akmer
Ranch Hand

Joined: Jul 06, 2010
Posts: 104
@swapnil kachave; You mean when you send primitive types only it work ok? Can you post the exception from the server's log?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
How is the service which is supposed to receive this Object defined?

Which RESTful service toolkit are you using?

Bill
Alok Bhandari
Greenhorn

Joined: Jun 11, 2008
Posts: 15
Hello , I am also facing the same problem that how can I send a BitSet or rather any other java object as parameter to a REST service.


SCJP 1.6(98%)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Since BitSet is Serializable, you can serialize it to a byte[] and send that. See java.io.ObjectOutputStream.

However, unless this is a totally in-house service in which both server and client are under your control, you will probably eventually regret not using a more general format.

Bill
Alok Bhandari
Greenhorn

Joined: Jun 11, 2008
Posts: 15
Hello William thanks for reply.What I am confused about is if I can have servlet and pass to it a java object through objectOutputStream and ObjectInputSteam,then Shall I go with servlet or have a REST service for this?

Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
IF you have a servlet that accepts a serialized object, you in fact have a start on a RESTful service - it is that simple, no toolkit required.

A servlet will be the entry point for any RESTful service since handling HTTP methods is what is required and thats what servlets are designed for.

Bill
Alok Bhandari
Greenhorn

Joined: Jun 11, 2008
Posts: 15
Hello Bill,

thanks for the reply , what I understand from your reply is that I can use REST service instead of servlet in this scenario and it gives me the same bahaviour as I expect.I just have one small query can you tell me what MIME type should be specified in "consumes" tag for accepting the java object as an input.And if you have link to any good tutorial for this then please can you send its link to me.thanks for your time and reply.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
I'm assuming you are using a toolkit like jersey that uses Java annotations to map requests to methods. In order to route the request to the right method, the mime type should match the type you specify in the request headers.

A content type of "application/octet-stream" seems pretty general.

Bill


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending java Object to REST web services.