Generating SOAP webservice client stubs using Apache-CXF, Maven, Eclipse

How to create WSDL-first Java SOAP webservice client using CXF, Maven, and Eclipse.

Recently we wanted to build a Soap webservice client to invoke some operations in the production server. The client is supposed to be used by support staff and they have some determined set of operations to be performed on a daily basis. It turned out giving them a jar with command line options required for their operations.
In this post let me try to show how to generate the client stubs and invoke an operation. We will use CXF tool called wsdl2java to turn the WSDL into Java client code and Maven for dependencies and generating the code. We will use Eclipse as the IDE.

1. SOAP Service WSDL for our example:
For this example let us get the CDyne free GetStockQuote WSDL from here. You can right-click the link and save the WSDL to a location in your system.

2. Eclipse Maven Project and POM file:

  • Create a new Maven project in eclipse with artifactId as stockquote. See pom below for the options.
  • Place the downloaded WSDL at location stockquote/src/main/resources/
  • Add the maven dependencies and the required plugins as below.

 <project xmlns="" xmlns:xsi=""  
      <name>Stock Quote client</name>  
           <!--Generate Java stub from WSDL at build time -->  
                <!-- Add the generated sources, this avoids having to copy generated sources to build location -->  

  • If you get undefined element declaration exception, please refer here to fix it.
  • We use cxf-codegen-plugin to run wsdl2java to generate our Java code into ${basedir}/generated/cxf/src/main/java
  • In eclipse java code is generated using Maven generate-sources. Right click the project stockquote -> Run as -> Maven generate-sources.
  • On successful build the source files will be generated in the new folder /generated/cxf/src/main/java.
  • Four client are generated as DelayedStockQuoteHttpGet_DelayedStockQuoteHttpGet_Client, ...HttpPost_Client and ...Soap_Client, ...Soap12_Client version. See eclipse screenshot below:

3. Running the Client:
Now that we have the client code generate, let us try to check if it's working.
Update one of the client code to check if the request/response works fine.
Sample code after updating DelayedStockQuoteSoap_DelayedStockQuoteSoap_Client:

 DelayedStockQuote ss = new DelayedStockQuote(wsdlURL, SERVICE_NAME);  
     DelayedStockQuoteSoap port = ss.getDelayedStockQuoteSoap();   
     System.out.println("Invoking getQuote...");  
     java.lang.String _getQuote_stockSymbol = "VZ";  
     java.lang.String _getQuote_licenseKey = "0"; _getQuote__return = port.getQuote(_getQuote_stockSymbol, _getQuote_licenseKey);  
     System.out.println("getQuote.result = " + _getQuote__return.getCompanyName());  
     System.out.println("getQuote.result.Verizon.lastTradeAmt = " + _getQuote__return.lastTradeAmount);  
     System.out.println("Invoking getQuoteDataSet...");  
     java.lang.String _getQuoteDataSet_stockSymbols = "";  
     java.lang.String _getQuoteDataSet_licenseKey = ""; _getQuoteDataSet__return = port.getQuoteDataSet(_getQuoteDataSet_stockSymbols, _getQuoteDataSet_licenseKey);  
     System.out.println("getQuoteDataSet.result=" + _getQuoteDataSet__return);  

In eclipse right click on the file and select "Run As" --> "Java Application".
Sample result as below:
 Nov 12, 2016 11:52:45 PM org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL  
 INFO: Creating Service {}DelayedStockQuote from WSDL: file:/C:/mysoftware/eclipse_wrkspce/stockquote/src/main/resources/delayedstockquote.wsdl  
 Invoking getQuote...  
 getQuote.result = Verizon Communications Inc. Com  
 getQuote.result.Verizon.lastTradeAmt = 46.69  
 Invoking getQuoteDataSet...$GetQuoteDataSetResult@9573584  
 Invoking getQuickQuote...  

Looks like our client is fetching results...
Src code in GitHub.
Next(in another post) we will see how we can build a jar with cmd line options to make some soap request/response.

No comments:

Post a Comment