GeeCON Prague 2014*
The moose likes Web Services and the fly likes Java Collections Usage in Webservice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "Java Collections Usage in Webservice" Watch "Java Collections Usage in Webservice" New topic
Author

Java Collections Usage in Webservice

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
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

Joined: Jan 10, 2002
Posts: 61310
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
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
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
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.


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

In whatever way that client code deems suitable for its purposes using the constructs available in the client language.
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

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

Joined: Jul 26, 2011
Posts: 462
    
    4
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

Joined: Mar 12, 2013
Posts: 296

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
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
That, to me, is still a data type defined in XML Schema. Maybe "data type defined using XML Schema" is clearer.
 
GeeCON Prague 2014
 
subject: Java Collections Usage in Webservice