How to config WebService to return ArrayList inste

2019-07-15 10:14发布

问题:

I have a web service in java that implemented on jax-ws. This web service return an generic list of User. It's working very good :).

@Stateless(name = "AdminToolSessionEJB")
@RemoteBinding(jndiBinding = "AdminToolSessionRemote")
@Remote(AdminToolSessionRemote.class)
@WebService
public class AdminToolSessionBean implements AdminToolSessionRemote {
...
@WebMethod(operationName = "GetAllUsers")
@WebResult(name = "AllUsers")
public List<User> getAllUsers() {
    return userSessionRemote.getAllUsers();
}
...
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User")
public class User extends BasicDataTransferObject {
...
@XmlElement(name = "Groups")
private List<Group> groups;
...
}

But I will use this web service in .Net Applications. When I add this web service as a wcf service or web service in VS2005 or VS2008 or VS2010, VS generate array instead od generic list 'Group[] Groups'. I change 'Collection Type' in 'Configuration Service Reference ...' dialog to 'System.Collections.Generic.List' but VS generate array :(.

I need to VS generate generic list or ArrayList, what I should to do?

回答1:

I'm not sure how to define this in java. However, in my C# service, my main transaction parameter (a purchase order) contains a list of line items defined like this:

private LineItems LineItemsField;
[DataMember(Order=13, EmitDefaultValue=false)]
public LineItems LineItems {
    get { return this.LineItemsField; }
    set { this.LineItemsField = value; }
}

LineItems is another C# class, defined like this:

[CollectionDataContract(Namespace="")]
public class LineItems : List<LineItem>
{
}

LineItem is the actual class that contains the line item fields.

The LineItems appears in WSDL as:

<s:element minOccurs="0" maxOccurs="1" name="LineItems" type="tns:ArrayOfLineItem" />

ArrayOfLineItem is defined as:

<s:complexType name="ArrayOfLineItem">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="unbounded" name="LineItem" nillable="true" type="tns:LineItem" />
    </s:sequence>
</s:complexType>

And of course, the LineItem class itself is defined elsewhere. Hopefully that helps.



回答2:

I would recommend not using language specific constructs in a webservice, since a webservice is meant to be language agnostic.

If you really want to, but you shouldn't you will have to write a specific serializer/deserialiser of which you can not be sure that it will work in all language.

I would just stick to an array and then write a wrapper in yo language of choice.