You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

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");

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

DynamicClientFactory

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 for it like this:

DynamicClientFactory dcf = DynamicClientFactory.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 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:

URLClassLoader classLoader = ...;
DynamicClientFactory dcf = DynamicClientFactory.newInstance();
Client client = dcf.createClient("people.wsdl", classLoader);
 
Object person = classLoader.loadClass("com.acme.Person").newInstance();
 
Method m = person.getClass().getMethod("setName", String.class);
m.invoke(person, "Joe Schmoe");
 
client.invoke("addPerson", person);

COMING SOON: Groovy Web Services support for dynamic clients so you don't need to use reflection!

  • No labels