We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 22,860 times

Contents

Related Categories

Web Services Interoperability between J2EE and .NET - Part 2 - An array with null elements

Wangming Ye

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.

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 merged into IBM), and then as one of the main developers of the WebSphere Content Distribution Framework in the WebSphere Edge Server group. He currently provides technical enablement for WebSphere business partners in the WebSphere Competency Center in the IBM Business Partner Technical Enablement organization.

Comments

  • Null parameters

    Posted by nrsimoes on 16 Jan 2006

    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 val...

  • But how... ?

    Posted by stefantura on 14 Jul 2005

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