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.

AddThis

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

Leave a comment

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

Related discussion

Events coming up

  • Oct 14

    What’s New in Visual Studio 2008 Service Pack 1?

    Birmingham, United Kingdom

    “Service Pack? We’re calling it a Service Pack? Are you kidding??!?!” Visual Studio 2008 Service Pack 1 will release later in 2008 alongside .NET Framework V3.5 Service Pack 1 and, together, they represent a significant upgrade to Visual Studio 2008. There are enhancements across many areas of the .NET Framework such as data access, windows application development and web development and there are also corresponding changes in the development environment to support the new framework features.