When Harry met Sally

Posted on Wednesday 12 August 2009

ryan460

I’ve been working on a WCF solution for a while now and I’ve been putting off answering the inevitable question: How interoperable is my service? Up until now we’ve used WCFStorm as a test harness which has been a really great integration test harness, however it’s essentially a .Net client onto my .Net services.

So I’ve decided to dip my wick into Java to see how easy it is to call my endpoints from a Java client/service.  I’ve decided to use NetBeans (6.7.1) with Metro (1.4) as my tool set (based upon a recommendation) and here are a few things I’ve learnt along the way….

1 Adding a web service client.

This is really easy, once you’ve created a project then right click on the project name and select new web service client:

jax1

JAX-WS (wsGen) will do the nuts and bolts to crank out the java proxy objects. You’ll now have a web service reference and generated source element in your project tree:

jax2

2. Using the web service

A  really nice function in NetBeans is the ability to drag the service reference into your code – Select the method on the service you’d like to invoke (from the project panel) and drag it into your class – NetBeans will stub out the basic structure of the call. If you have a simple service then you’re almost ready to go – other than initialising any parameters required by the service. In my case my interface is complex and I want to know how Java has translated my types into java types.

3. WCF to Java Type fun

In the following code from my Java class you can see I am initialising an object LocationIdentifier which I need to pass as a parameter to my service. What’s funny about this initialisation?

            LocationIdentifier lid = new LocationIdentifier();
            lid.setEntityId(JAXBHandler.createString("9999"));
            lid.setSystemId("TestSystem");

What the hell is this JAXBHandler? Well this is a nasty little class of mine which handles the creation of JAXBElement<> types. But what is different between the entity id property and the system id property on LocationIdentifier? Very little –  both properties are of type string, however wsGen interprets these differently depending upon the type definition in the wsdl.  So why is WCF generating these members differently in the wsdl?

When the wsdl defines an element having both minOccurs=”0″ and  nillable=”true” then wsGen creates a proxy object with a JAXBElement<> type instead of a standard java type. More discussion can be found here. In WCF when a type is nullable i.e. String then WCF will generate the WSDL element for this type with a property nillable=”true”.  Class properties in WCF which are exposed via the WCF DataMember attribute can optionally include a parameter IsRequired which can be used to control the occurance of minOccurs in the wsdl.

So if we look at the LocationIdentifier class in .Net:

...
        [DataMember(Name = "SystemId",IsRequired=true)]
        public string System
        {
            get { return system; }
            set{system = value;}
        }
...
        [DataMember(Name = "EntityId")]
        public string Id
        {
            get { return id; }
            set{id = value;}
        }

This manifests itself in the wsdl like:

&lt;xs:element minOccurs="0" name="EntityId" nillable="true" type="xs:string"&gt;
&lt;xs:element name="SystemId" nillable="true" type="xs:string"&gt;

Depending upon your preferences you can work with JAXBElement<> types however it makes life much easier for a Java developer who uses your WCF service if they can work with the more obvious Java types. Overall it’s been really easy to get going and test my service from a Java client, I like both NetBeans and Metro especially over previous experiences I’ve had with Eclipse. If I’m not careful I’ll become a grumpy dissatisfied Java developer!

4 Comments for 'When Harry met Sally'

  1.  
    August 13, 2009 | 12:48 pm
     

    We are only grumpy when we have to use .NET and Visual Studio ;)

  2.  
    mapbutcher
    August 13, 2009 | 1:04 pm
     

    ……the force is strong in you Steve, these forays away from the darkside are a rewarding experience.

  3.  
    August 13, 2009 | 3:15 pm
     

    I am glad you enjoyed netbeans – I am really enjoying using 6.7.1 right now. Writing some JavaScript and the code completion just rocks. I want to check out Jersey for making REST services.

  4.  
    mapbutcher
    August 13, 2009 | 3:23 pm
     

    Yep, NetBeans is a nice IDE – I’ve also got a todo to look at the REST support in WCF, however I think the challenge will be designing the resource representations rather than the technology

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI