CXF Client overview
In previous chapter, we introduced get starting CXF web service example and CXF interceptor, CXF also provides you with many options to build CXF clients, today we will have quick view how to develop web Service client.
Prerequisites
To enable CXF client the first common scenarios is that you have successfully developed web service, the WSDL is published. WSDL is an XML format used to described Web Services Language.
In this case most of usually way is to generate CXF client from WSDL. The WSDL2Java tool will generate JAX-WS clients from your WSDL, it is very strongly typed interface. Alternatively, if you use IntelliJ IDEA as your IDE, please choose your project source code folder then right-click, select “web service->Generate Java code from WDDL or WADL” options.then just follow the below:
Note: web service WSDL url specific the CXF web service publishing address.
If generate successfully, your project have a series new generated CXF client class, typical usage of it will look like so:
SimpleWebService service = new SimpleWebService (); SimpleWebServiceType client = service.getSimpleWebServicePort(); List< Attribute> result = client. getAttributes ( new getAttributes("atturibute","value"));
The cxf client is really very simple, alternatively, you also can use another way to develop the cxf client:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(INEWSSystemPortType.class); String wsdlPort = “http://localhost/SimpleWebService.wsdl”; factory.setAddress(wsdlPort); SimpleWebServiceType Type= (SimpleWebServiceType) factory.create();
The two way to implement web service client exactly have some differences, the first way you must hardcode your wsdlPort into class SimpleWebService,Like:
static { URL url = null; try { url = new URL("file:/E:/SimpleWebService.wsdl"); } catch (MalformedURLException e) { e.printStackTrace(); } SIMPLE_WSDL_LOCATION = url; }
But the second way can fit many different situations, it is very flexible to set wsdlPort to JaxWsProxyFactoryBean instance.
Using JAX-WS Proxy to implement CXF client
Instead of using a wsdl2java-generated stub client directly, you can use Service.create to create Service instances, the following code illustrates this process
import java.net.URL; import javax.xml.ws.Service; ... URL wsdlURL = new URL("http://localhost/SimpleWebService.wsdl"); QName SERVICE_NAME = new QName("http://asjava.com/webservice", "SOAPService"); Service service = Service.create(wsdlURL, SERVICE_NAME); Greeter client = service.getPort(Greeter.class); String result = client.greetMe("test");
Using JAX-WS Dispatch APIs to implement CXF client
JAX-WS Dispatch APIs allow your dynamically invoke web service, so actually you don’t need to generate a client. it allow you create messages and dispatch them.
import java.net.URL; import javax.xml.transform.Source; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; ... URL wsdlURL = new URL("http://localhost/SimpleWebService.wsdl"); Service service = Service.create(wsdlURL, new QName("HelloService")); Dispatch disp = service.createDispatch(new QName("HelloPort"), Source.class, Service.Mode.PAYLOAD); Source request = new StreamSource("") Source response = disp.invoke(request);...