Library tutorials & articles

Web Services Interoperability between J2EE and .NET - Part 2

An array with null elements

The XML representations of an array with null elements are different between .NET and WebSphere. Consider the Java Web service method in Listing 6 .

Listing 6. A Java method returning an array with a null element

    public String[] returnArrayWithNull() {
        String[] s = new String[3];
        s[0] = "ABC";
        s[1] = null;
        s[2] = "XYZ";
        return s;
    }

The String element, s[1] , is assigned a null value. When a .NET client invokes this Web service method hosted on the WebSphere platform, the String array is serialized as:

Listing 7. The Web service response message from WebSphere

<soapenv:Body>
<returnArrayWithNullResponse xmlns="http://array.test">
<returnArrayWithNullReturn>ABC</returnArrayWithNullReturn>
<returnArrayWithNullReturn xsi:nil="true"/>
<returnArrayWithNullReturn>XYZ</returnArrayWithNullReturn>
</returnEmptyStringResponse>
</soapenv:Body>

The second element in the array is set to xsi:nil="true" . That's fine in Java; a Java client would have correctly de-serialized it back to a null String for the second element in the array. However, the .NET client de-serializes it into a zero length string instead of a null string. Empty and Null are completely different beasts in any object-oriented programming language.

Now consider another Web service method hosted on WebSphere, as shown in Listing 8.

Listing 8. A Java method with arrays and its input and output signatures

    public String[] processArray(String[] args) {
        //do something to the input array and return it back to the client
        return args;
    }

This time, the Web service method takes an array as input, processes it, and returns the array back to the client. Suppose a .NET client sends out an array with a null element as shown in the code in Listing 9.

Listing 9. A .NET client sends an array with a null element

        TestArrayService proxy = new TestArrayService();
        string[] s = new string[3];
        s[0] = "abc";
        s[1] = null;
        s[2] = "xyz";
            // Console.WriteLine("the length of the input array = " +
            s.GetLength(0));
        string[] ret = proxy.processArray(s);
            // Console.WriteLine("the length of the output array = " +
            ret.GetLength(0));

Listing 10 shows the SOAP request message from the .NET client.

Listing 10. The SOAP request message sent by the .NET client

<soap:Body>
<processArray xmlns="http://array.test">
<args>abc</args>
<args>xyz</args>
</processArray>
</soap:Body>

The null element, s[1] , is omitted from the SOAP request sent by the .NET client. As a result, the length of the returned array is no longer the same as the length of the original array. If this array's length or element index is important to the client's logic, then the client will fail.

The best practice is not to pass an array with null elements between Web service clients and servers.

Comments

  1. 16 Jan 2006 at 09:50

    I'm developing a .NET Web Service that have some functions with datetime parameters but the I can't call it from Java client with nulls. Can you explain me how to define a complex type to wrap the value type and set the complex type to be null to indicate a null reference?


    Thanks

  2. 14 Jul 2005 at 13:21
    Hi,

    Nice article - but what sould go next - some examples (or maybe names of solutions) of software which translates between different SOAP datatypes... There have to be such a frameworks for type matching, aren't these?

    --
    best regards
    stic
  3. 01 Jan 1999 at 00:00

Leave a comment

Sign in or Join us (it's free).

Wangming Ye Wangming Ye is an IBM Certified Enterprise Developer and a Sun Certified Enterprise Architect for J2EE Technology. He began as a developer in the DCE/DFS department at Transarc Corporation (later m...

Related podcasts

  • Open Source SOA

    The focus of this session will be to demonstrate innovative open source technologies and give you an insight into the skills, tools and techniques for SOA-enabling your enterprise architecture. This session will be taught via lecture as well as interesting live demonstrations (e.g. .NET linking t...

Events coming up

  • Dec 15

    Portland Java User Group

    Portland, United States

    This month's topic: TBD----------PJUG meetings start with eat+meet+greet time (pizza and beverages are provided), followed by the featured speaker, then some time for Q&A, discussion, and sometimes a drawing to give away swag. :)It is...

Want to stay in touch with what's going on? Follow us on twitter!