Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sending java Object to REST web services.

 
swapnil kachave
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the service which is supposed to receive this Object defined?

Which RESTful service toolkit are you using?

Bill
 
Alok Bhandari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic