How to consume Web Service in CXF Using WSDL?

CXF is a service framework that help to build and develop web services using frontend programming APIs, like JAX-WS. It provides wsdl2java utility to create web service client stub classes using wsdl url.

Suppose we have “http://192.168.10.185/DemoService/Service.asmx?WSDL” wsdl url that has operations to add and multiply numbers.

To create client stub classes from wsdl2java utility, you need to run following command at command-line.

>wsdl2java -d clientDir(enter path where you need to generate stub classes) wsdlUrl

i.e. > wsdl2java -d D:\CXFSample\src http://192.168.10.185/DemoService/Service.asmx?WSDL


Above utility will generate following classes.

org\tempuri\AddNumbers.java

org\tempuri\AddNumbersResponse.java

org\tempuri\MultiplyNumbers.java

org\tempuri\MultiplyNumbersResponse.java

org\tempuri\ObjectFactory.java

org\tempuri\package-info.java

org\tempuri\Service.java

org\tempuri\ServiceHttpGet.java

org\tempuri\ServiceHttpPost.java

org\tempuri\ServiceSoap.java

Here Service.java class represents wsdl endpoint elements. And ServiceSoap.java is a interface that represents wsdl port type and have all operations declaration that are define by wsdl port type.

Following main class define the client implementation code to access above web service using generated stub classes.

public class Client {

private static final QName SERVICE_NAME = new QName(“http://tempuri.org/”, “Service”);

public static void main(String args[]) throws Exception {

URL wsdlURL = new URL(“http://192.168.10.185/DemoService/Service.asmx?WSDL”);

Service service = new Service(wsdlURL, SERVICE_NAME);

ServiceSoap port = service.getServiceSoap();

System.out.println(“Add numbers result …” +port.addNumbers(4, 5));

System.out.println(“Multiplications result …” +port.multiplyNumbers(4, 5));

}

}

150 150 Burnignorance | Where Minds Meet And Sparks Fly!