Contact Us 1-800-596-4880

Using a Web Service Client Directly

This page describes how to use a standalone CXF-generated client with Mule. This approach is different from the outbound router approach, which is typically a more appropriate fit for most applications.

If you plan to connect to an external web service that exposes a WSDL file, the recommended connector to use is the Web Service Consumer.

There are two ways to use CXF clients. First, you can generate a client from WSDL using the CXF WSDL to Java tool. Second, if you’ve built your service via "code-first" methodologies, you can use the service interface to build a client proxy object.

When using a CXF client, it automatically discovers the Mule instance (provided you’re in the same JVM/Classloader) and uses it for your transport. Therefore, if you’ve generated a client from WSDL, invoking a service over Mule can be as simple as the following:

HelloWorldService service = new HelloWorldService();
HelloWorld hello = service.getSoapPort();

// Possibly set an alternate request URL:
// ((BindingProvider) greeter).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                                                       "http://localhost:63081/greeter");
String sayHi = hello.sayHi();

Building a Client Proxy

Following is an example of how to construct a client using the service that was developed in Building Web Services with CXF:

QName SERVICE_NAME = new QName("http://server.hw.demo/", "HelloWorld");
QName PORT_NAME = new QName("http://server.hw.demo/", "HelloWorldPort");

Service service = Service.create(SERVICE_NAME);

// Endpoint Address
String endpointAddress = http://localhost:63081/hello";

// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

HelloWorld hw = service.getPort(HelloWorld.class);

System.out.println(hw.sayHi("World"));