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

<returnArrayWithNullResponse xmlns="http://array.test">
<returnArrayWithNullReturn xsi:nil="true"/>

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 = " +
        string[] ret = proxy.processArray(s);
            // Console.WriteLine("the length of the output array = " +

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

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

<processArray xmlns="http://array.test">

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.

You might also like...


About the author

Wangming Ye United States

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 (late...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An expert is a man who has made all the mistakes that can be made in a very narrow field” - Niels Bohr