• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Collections Usage in Webservice

 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Suppose I write a WebService in Java. And, the return type of that webservice is any Java collection say a Map<String, Object>. Now, as per the webservice definition, I can invoke this webservice from any technology code like C, Perl, C++, .net etc. Now, I am curious to know how that particular collection of Map<String, Object> will be handled by a C, C++, Perl code or any other language code where we don't have any that kind of collection available.

Thanks & Regards,
Vaibhav Garg
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By its very definition an HTTP response is text, so you cannot return any collection, Java, C C++ or otherwise. You must return text.

Therefore, your response needs to be encoded as text. Customary formats are XML or JSON, with the latter becoming more popular with non-SOAP web services; especially RESTful web services.
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear for your response.

That right that we will be returning the text as in XML. So, we will return the map or list as text through XML but I am wondering how this XML will regenerate the data structure back on client side where we have code in C/C++.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should restrict yourself to the data types defined in XML Schema, since only those are guaranteed to work cross-platform. On the Java side, that means arrays, not collections: http://www.ibm.com/developerworks/webservices/library/ws-tip-coding/index.html

Bear Bibeault wrote:By its very definition an HTTP response is text, so you cannot return any collection, Java, C C++ or otherwise. You must return text.

The problem is somewhat different that that. Using Java types as such is no problem, since they will automatically be mapped to XML by the SOAP framework. Using types that can not be mapped to one of the types defined XML Schema is a problem, though.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In whatever way that client code deems suitable for its purposes using the constructs available in the client language.
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav G Garg wrote:...I am wondering how this XML will regenerate the data structure back on client side where we have code in C/C++.

You could even put JSON in SOAP envelope and deserialize it on C/C++ side if it has JSON-library.
 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Rest webservice:

On the server side, if you create a Restful webservice (using any restful framework) and try to return a map,
the framework's runtime will convert the map into text-based json format before it travel thru http.

-Have a look at REST Jersey or Spring MVC for example.

On the client, it's easy to consume a text-baseD json format.

-Have a look at JQUERY for example or any language

2. Soap webservice:

Look at data type allowed by JAX-WS spec. See Metro implementaion for example.
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:You should restrict yourself to the data types defined in XML Schema, since only those are guaranteed to work cross-platform.

But it is also possible to define more complex data-type which reflects Map data-type. So any client code, that has XSD, can demarshall XML to objects (C++, Java, etc.). So define something like:
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That, to me, is still a data type defined in XML Schema. Maybe "data type defined using XML Schema" is clearer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic