Consuming Web Services with WSDL2JAVA

Paul Zolnierczyk (@paulish29)
march 4th, 2014

If you’ve come across the task of consuming a web service via a WSDL you were given, there’s a chance you may have cringed a bit. All that XML involved and then determining your approach is a challenge as well! Do you want to use a SOAPConnectionFactory or create an XML message by hand and parse the response? These approaches will work but it will likely take time away from what you really want to do and that is develop the application you’re working on. Along comes WSDL2JAVA, a tool which will build Java proxies and skeletons for services with WSDL descriptions. In this article I’ll highlight the steps needed to consume a web service with WSDL2JAVA.

Prerequisites

Optional: If you know how to use SoapUI, you can test the WSDL there.

We’ll be using the stock WSDL as an example.

Generate Java class files from WSDL

  1. In the IDE go to: File > New > Other. Look for Axis2 Wizards folder, expand it and select Axis2 Code Generator. Click Next.
  2. Select the radio button for “Generate Java source code from a WSDL file” and click Next.
  3. Paste in the URL of the WSDL we will be using in this example and click Next.
  4. In the “Codegen option” dropdown choose the “custom” option. This will allow you to check the checkbox for “Generate Test Case.” Select that checkbox and click Next.
  5. Select the radio button for “Browse and select location on local file system” and select the high level folder for your project in your workspace.
  6. Select the checkbox for “Add the Axis2 codegen jars to the codegen project”
  7. Select the checkbox for “Add Axis2 libraries to the codegen result project” and browse for the high level directory where you unpacked the axis2 library. If you hit “Check libs…” it should return “Axis libs loaded successfully !!”
  8. If you want this to be packaged in a convenient jar file then go ahead and select “Create a jar file of codegen result project and add to the resulted project lib folder” and give it a meaningful name.
  9. Click “Finish”, at this point it should begin generating the Java files.
  10. You may need to refresh your project in Eclipse/STS to see the generated files, mine are in a package com.cdyne.ws
  11. You may also need to add the axis2 jars to your project build path in the event you are seeing Eclipse build errors.
  12. The reason I have you create the test classes is so you can open up the test file and get an idea on how to invoke the web service you are targeting. In this example, let’s execute “GetQuickQuote” so take a look at the testgetQuickQuote() method in the test file. You can kind of work backwards here, you know you have to create a quickQuote object and set the stock symbol and license key (the license key needs to be an empty string).
  13. Now that you see how it’s supposed to be executed, let’s do a quick and dirty test by creating a new class in the src folder called “StockDriver” and include the static void main method in there.
  14. Just for brevity, here is some code you can copy and paste that you should be able to execute:
import com.cdyne.ws.*;

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

DelayedStockQuoteStub stub = new DelayedStockQuoteStub();
GetQuickQuote quickQuote = new GetQuickQuote();

quickQuote.setStockSymbol("GOOG");
quickQuote.setLicenseKey("");

GetQuickQuoteResponse quickQuoteResponse = stub.getQuickQuote(quickQuote);

System.out.println(quickQuoteResponse.getGetQuickQuoteResult());
}
}

Running this should give you a result, today (04 March 2014) it gave me 1202.69.

That’s all there is to it! Now apply these steps to your project and start using those web services!

Tags: technology java web-services