Wednesday, January 23, 2008

Web Service Clients in Browsers

A quick Google reveals several toolkits for talking to Web Services from JavaScript. The purpose of this post is to toot the horn of a new one. Apache CXF version 2.1 will include support for browser-resident JavaScript clients, and you can try it out, right now, by downloading a snapshot.

What's so special about the CXF JavaScript support?

To begin with, it really does handle standard-conforming web services. Several of the other packages out there lack true support for XML namespaces and XML schema. Some of them require, for example, that the messages use fixed prefixes for XML namespaces, contrary to the XML spec. It handles some of the more complex XML schema cases (xs:any or xs:anyType), though there are some extreme cases (xs:choice) that it doesn't do.

Along with the rest of CXF, it comes under an Apache license, so that those of you with an allergy to the GPL need have no fear.

It offers a very convenient mechanism for delivering the JavaScript code to the browser. A URL like:
delivers the actual JavaScript. Put that URL in a script tag, and you're all set.

It handles MTOM attachments. Why would you want MTOM attachments in a browser, where you can't have any binary at all? You would want them to attach non-XML-1.0 text without paying the base64 tax.

Note that this is a code-generation scheme. CXF generates a JavaScript 'class' (a constructor with some functions on its prototype) for your service, and more JavaScript classes for your bean classes. Incoming messages are deserialized from XML into these objects, and outgoing messages get the reverse treatment. It is not a dynamic scheme in which you hand in a set of parameters and the code tries to map them to some operation of a service.

Using full web services gives you much more flexibility than using REST or other simplified mechanisms. If you have a use for all of that, please give the CXF JavaScript facility a whirl.

No comments: