DooDoo
DooDoo

Reputation: 13457

Calling a web service that accept array or list of a class in Java

I want to create a web service using C#. In web service I have a web method that accept list of a specific class:

[DataContract]
public class CompositeType
{
    string stringValue = "Hello ";

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }

    [DataMember]
    public List<Product> Products { get; set; }
}

[DataContract]
public class Product
{
    [DataMember]
    public int PID { get; set; }

    [DataMember]
    public string PName { get; set; }
}

and my web method:

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

I want to publish this service using BasicHttpBinding that Java users can call it too. Now since the Java programmer is not around me, I wanted to ask those who have the experience to do this :

1) Can Java programmers call my web method that accept List<Product>?

2)Should I change List to Array?

Thanks for you contribution

Upvotes: 0

Views: 272

Answers (2)

Abraham Qian
Abraham Qian

Reputation: 7522

As with .Net client calling WCF using the Svcutil tool, most Java users use the asis2 library which is a webservice engine to invoke web service.
WebService is a specification that any service that implements it can be called WebService. they use SOAP message based on XML to communicate. they use WSDL to describe the service details, which is used for generating the client proxy class. The reason why WCF can be called across service boundaries by various platforms is that it is also a web service. Although there may be different data types on various platforms, as long as we specify how to represent it in XML and how to serialize it, the service can be called correctly by others platforms, By default, List is specified to be serialized using a one-dimensional array.

Upvotes: 1

Presumably your HTTP API serializes this as JSON (or maybe XML). In either case, libraries such as Jackson can handle it just fine, and most REST clients will even handle that part automatically. Standards compliance is the rule, and so as long as your List<Product> is converted to/from a regular JSON array, everything should work smoothly.

JSON doesn't have separate list types, just the plain array, so either array or list-based serialization should be equivalent.

As a note, most APIs use either camelCase or snake_case for properties, so your property names (in JSON) would be expected to be stringValue, products, pid, and pName.

Upvotes: 1

Related Questions