JavaScript Clients

JavaScript client support is a feature of CXF 2.1.

CXF provides a capability to generate JavaScript code that can communicate with a web service via the common XMLHttpRequest object. This object is present in modern browsers. CXF also includes an implementation that plugs into Rhino (though this is not conveniently packaged as of the current writing.)

Introduction: Why would you want this (in a browser)?

If you are writing an application that just happens to be in JavaScript, and you want it to communicate with a web service, then the answer to this question is probably self-evident. The more interesting question is, "Why would you want to talk to a web service via a SOAP binding from a browser?"

Historically, much attention has been paid to bindings and protocols that are not SOAP for browser-server communications. The writing about this often starts from a common idea: 'It can't possibly be practical to handle the complexity of full XML/SOAP messaging in JavaScript.'

The authors of the CXF JavaScript client module are not the first, or by any means the only, people to reconsider this idea. There are several alternatives out there with varying levels of capability and license. If you are familiar with SOAP web services, then you may find it easier to build and understand code that talks to a JavaScript binding for standard Document/Literal or RPC/Literal services than to take on board yet another framework, such as REST.

If, on the other hand, you are happy using REST or something like it, please don't be offended by this discussion. CXF is happy to accomodate.

What is the Capability?

The CXF JavaScript client facility is a code generator. Given an abstract model of a web service with a SOAP binding, it will produce a set of JavaScript classes to talk to it. You can think of it as 'WSDL to JavaScript.' However, CXF does not force you to use a WSDL file to generate a JavaScript client. CXF supports all of the following:

  • wsdl2js - reads a WSDL file, write JavaScript.
  • java2js - 'reads' Java code, using one of the CXF front ends (simple or JAX-WS) and one of the supported data bindings (Aegis or JAXB), and write JavaScript.
  • dynamic JavaScript - responds to a URL of the form http://host:port/port_and_or_service?js with the JavaScript code to talk to the service.

The dynamic mode is convenient: it avoids the need to generate and manage JavaScript files. You will probably find it helpful to generate some JavaScript files, however, and read them in order to understand what to code.

As a code generator, the JavaScript client generator is parallel to JAXB or JAX-WS. It defines a mapping from an abstract model of a web service to JavaScript objects. Unlike JAXB and JAX-WS, there is no committee that has standardized a 'JavaScript binding.' The CXF binding may not be to everyone's tastes.

The CXF JavaScript binding is only concerned with interfaces and operations. It is completely oblivious to 'services' and 'ports'. The programmer is expected to know the URL of the service, and that's that.

The Details