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

Compare with Current View Page History

Version 1 Next »

Sometimes when you work with web services, you don't want to have generate a client at build time. Its much more convenient to create a client at runtime and use it dynamically. CXF has a DynamicClientFactory class designed for just this.

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 = book.getClass().getMethod("setName", String.class);
m.invoke(book, "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