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 Implimenting Collections In WebServices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Implimenting Collections In WebServices" Watch "Implimenting Collections In WebServices" New topic
Author

Implimenting Collections In WebServices

Muhammad Ahsan Jamshaid
Ranch Hand

Joined: Jun 01, 2006
Posts: 59
Hi Friends!
i am implimenting a java service as an web service using axis. I m using wsdl2java to generate stubs and skeltons. The problem i m facing is returning collections from the service. I did my R&D and come to the solution that i must return arrays instead of List etc. That solution is correct and its works but i was wondering that is there any way to impliment list as return type in wsdl? so that it could work for atleast java clients. Please share ....

--Thank you..
-- jamshaid..


--Ahsan Jamshaid... SCJP 5(80%)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41525
    
  53
If you use data types not defined in XML Schema (which is what WSDL uses to describe datatypes), you're giving some of the benefits of type checking, in addition to cross-platform compatibility. Why would you want to do that?


Ping & DNS - my free Android networking tools app
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Given the ease with which you can convert between List and array representations, there is no good reason to transmit Lists in web services. Think about it - most clients are going to use a List like an array anyway.

Bill
Muhammad Ahsan Jamshaid
Ranch Hand

Joined: Jun 01, 2006
Posts: 59
thanks for replying friends..

you are right that list must be sent as an arrays. but the thing is i just want to know that is there any mechnisim by which we can impliment collection returns?

-- jamshaid
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by Muhammad Ahsan Jamshaid:
i just want to know that is there any mechnisim by which we can impliment collection returns?


Your point of view is a Java-centric point of view; web services don't care about Java or any other host language they are implemented in (as Ulf has already pointed out). SOAP web services use the XML Schema sequence where you use collections in Java. However the simplest Java type that can represent a sequence is in fact the array (it is the lowest common denominator) - so arrays were chosen to represent a sequence. And as Bill has already pointed out there are plenty of convenience functions to turn an array into a List (Arrays.asList) and a List into an array (List.toArray) which makes implementation of a wrapper that consumes a produces Lists fairly simple.

Some tools (e.g. JAXB) may provide you with a facility for coercing those XML Schema sequences into a collection of your choice instead of an array. However that choice is captured either in the tool's binding file or in some Java code annotation but not in the WSDL.

That being said, JAX-WS supports inline-customizations as shown in AddNumbers.wsdl. However this is no longer a WSDL in the conventional sense as it contains information that can only be used by some clients - and even those that can use it may want to ignore it. I think the inline-customization use case is one where you make a copy of the original WSDL and then customize it for your own purpose before you perform any WSDL-to-Java code generation.
[ February 12, 2008: Message edited by: Peer Reynders ]
Suryavas.k Kazipeta
Greenhorn

Joined: Nov 24, 2008
Posts: 3
Hi,

Implementing java.util.Collections is possible in WebServices using @XmlJavaTypeAdapter (JAXB 2.0).
have a look on below link and example
https://jaxb.dev.java.net/guide/XML_layout_and_in_memory_data_layout.html

@XmlRootElement
class ComplexStruct {
@XmlJavaTypeAdapter(value=ArrayListDataAdapter.class)
@XmlElement
ArrayList<Data> dataList;
}

class ArrayListDataAdapter extends XmlAdapter<Data[], ArrayList<Data>> {

@Override
public ArrayList<Data> unmarshal(Data[] objectArray2ArrayList) {
if ( objectArray2ArrayList == null )
return null;

return new ArrayList<Data>(Arrays.asList(objectArray2ArrayList));
}
@Override
public Data[] marshal(ArrayList<Data> arrayList2ObjectArray) {
if ( arrayList2ObjectArray == null )
return null;

Data[] dataArray = new Data[ arrayList2ObjectArray.size() ];
int i=0;
for (Data data : arrayList2ObjectArray) {
dataArray [ i++ ] = data;
}
return dataArray;
}
}

regards,
Surya.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implimenting Collections In WebServices