Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

The usual way to construct a web service client is to include the Java interface for the service (the SEI) and any classes that are used for inputs and output in the client application. This is not always desirable or practical.

CXF supports several alternatives to allow an application to communicate with a service without the SEI and data classes. JAX-WS specified CXF provides several ways to invoke services dynamically at runtime - that is, without generating a client from the WSDL. This guide covers usage of the Client interface to interact with a service. Another option you may wish to investigate is the JAX-WS Dispatch API.

Sometimes when you work with web services, you don't want to have to generate a client at build time. Its much more convenient to create a client at runtime and use it dynamically. CXF includes a Client interface which allows you to invoke operations and pass parameters for those operations. For instance:

...


Client client = ....;
Object[] result = client.invoke("sayHi", "Dan");

, as well as the Provider interface for reading and writing XML. This page, however, describes the dynamic client facility of CXF. With dynamic clients, CXF generates SEI and bean classes at runtime, and allows you to invoke operations via APIs that take Objects, or by using reflection to call into full proxies.

Note that, in general, CXF only supports WSI-BP services. If you attempt to create a dynamic client for a WSDL that uses features outside of WSI-BP, CXF may throw an exception.

DynamicClientFactory and JaxWsDynamicClientFactory

CXF provides two factory classes for dynamic classes. If your service is defined in terms of JAX-WS concepts, you should use the JaxWsDynamicClientFactory. If you do not want or need JAX-WS semantics, use the DynamicClientFactory. The remainder of this page uses the JaxWs version.

There are two ways to create Clients. The first would be through the ClientFactoryBean and JaxWsClientFactoryBean classes. The second is through the DynamicClientFactory. The DynamicClientFactory goes the additional step of generating and compiling JAXB POJOs in the background for use at runtime via reflection. This is most useful when you're using a dynamic language such as Groovy with CXF.

ClientFactoryBeans

TODO

...

Let's pretend for a moment that you have a WSDL which defines a single operation "echo" which takes an input of a string and outputs a String. You could use the DynamicClientFactory JaxWsDynamicClientFactory for it like this:

Code Block
java
java
DynamicClientFactoryJaxWsDynamicClientFactory dcf = DynamicClientFactoryJaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("echo.wsdl");

Object[] res = client.invoke("echo", "test echo");
System.out.println("Echo response: " + res[0]);

Many WSDLs will have more complex types though. In this case the DynamicClientFactory JaxWsDynamicClientFactory takes care of generating Java classes for these types. For example, we may have a People service which keeps track of people in an organization. In the sample below we create a Person object that was generated for us dynamically and send it to the server using the addPerson operation:

Code Block
java
java
URLClassLoader classLoader = ...;
DynamicClientFactoryJaxWsDynamicClientFactory dcf = DynamicClientFactoryJaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("people.wsdl", classLoader);

//The context class loader has been reset by the dynamic client factory's create client codeObject 
Object person = Thread.currentThread().getContextClassLoader().loadClass("com.acme.Person").newInstance();

Method m = person.getClass().getMethod("setName", String.class);
m.invoke(person, "Joe Schmoe");

client.invoke("addPerson", person);

You may be asking yourself the following question: "Where did the class name 'com.acme.Person' come from?"

One way to get the class names is to run wsdl2java and examine the results. The dynamic client factory uses the same code generator as that tool. Another way is to walk the CXF service model. This has the advantage that it delivers Class<?> objects directly, COMING SOON: Groovy Web Services support for dynamic clients so you don't need to use reflection!obtain the correct class loader reference and run loadClass.

The wsdl_first_dynamic_client sample uses this approach. Read the file 'ComplexClient.java' to see the process, which uses some of the java.bean classes to simplify the code slightly.

Note
titleNote

The JaxWsDynamicClientFactory sets the Thread context ClassLoader to a new ClassLoader that contains the classes for the generated types. If you need the original ClassLoader, make sure you save it prior to calling createClient.