
SoapUI Cookbook
By :

This recipe shows how to configure SoapUI (Apache CXF) tool integration to generate a runnable Java web service with an empty implementation using its WSDL. This could be useful if you need a quick menu-driven way to create a SOAP web service that can be implemented and deployed separately to SoapUI.
The WSDL that we are going to use defines a simple invoice service. It has only one operation to retrieve a basic invoice document using its invoice number:
getInvoice
invoiceNo : string
InvoiceDocument (invoiceNo : string, company : string, amount : string)
http://localhost:9001/ws/invoice/v1
The WSDL can be found at soap/invoicev1/wsdl/invoice_v1.wsdl
in this chapter's sample code.
We'll need the Apache CXF web service framework to generate the web service stub using SoapUI tooling. Download the latest version from http://cxf.apache.org/download.html (I have used version 3.01).
Apache CXF Version
Despite the tool menu stating version 2.x, you can go for the latest version, which, at the time of writing, is 3.01 (requires JDK 1.7+). Otherwise, choose version 2.7.x for JDK 1.6+ support, or version 2.6.x for JDK 1.5 support.
To build and run the service Java code, the minimum you will need is a suitable JDK. I have used JDK 1.7.0_25. Optionally, you may also want to use an IDE like Eclipse to make easy the work of exploring, building, and running the generated web service code.
Other SoapUI Tools
While you are free to choose any alternate framework supported by SoapUI tools (see http://www.soapui.org/SOAP-and-WSDL/code-generation.html), note that although the principles will stay the same, the command details and the resulting generated web service artifacts will of course vary.
First, we need to configure SoapUI to be able to generate and build the invoice web service. Then, we can run it as a standard Java executable. Perform the following steps:
bin
directory, and then click on OK:<chapter1 samples>/soap/invoicev1/wsdl/invoice_v1.wsdl
.<chapter1 samples>/soap/invoicev1/src/main/java
.ws.invoice.v1
.<chapter1 samples>/soap/invoicev1/target/classes
.invoice_v1.wsdl
should not produce any output with this option.–wsdlLocation invoice_v1.wsdl
in Tool Args. This tells the web service code where to look for the WSDL file at runtime. Setting the value like this means that invoice_v1.wsdl
is expected to be the root of the classes
directory. More on this in the next section.You should also see the following generated Java source files in your output folder, for example:
<chapter1 samples>/soap/invoicev1/src/main/java/ws/invoice/v1/ InvoiceDocumentType.java InvoicePortType_InvoicePort_Server.java ObjectFactory.java InvoicePortImpl.java InvoiceRefType.java package-info.java InvoicePortType.java InvoiceServiceV1.java
The corresponding class files in your class folder, for example:
<chapter1 samples>/soap/invoicev1/target/classes/ws/invoice/v1/
Mac/Linux Issue
I suspect that there is a minor SoapUI bug here. If you get an error like sh: ./wsdl2java.sh: No such file or directory
, then an easy fix is to open a shell in <Apache CXF Home>/bin/
and copy wsdl2java
to wsdl2java.sh
; for example, cp wsdl2java wsdl2java.sh
.
invoice_v1.wsdl
into the classes folder location, for example, into <chapter1 samples>/soap/invoicev1/target/classes
. Otherwise, when the server is run, you will see an error like [failed to localize] cannot.load.wsdl(invoice_v1.wsdl)
.cd <chapter1 samples>/soap/invoicev1/target/classes java ws.invoice.v1.InvoicePortType_InvoicePort_Server Starting Server Server ready...
To confirm whether it's actually working, open a browser and go to http://localhost:9001/ws/invoice/v1?wsdl
, and you should see the (invoice_v1.wsdl
) WSDL displayed. Our generated server is up and running.
All that SoapUI is actually doing is building command-line parameters for the various web service frameworks to do the generation. In this example, those happy with the command line could just run <Apache CXF Home>/bin/wsdl2java
directly.
Apache CXF wsdl2java script
For more info on the wsdl2java
options, see http://cxf.apache.org/docs/wsdl-to-java.html.
Let's take a quick look at the generated source files. The main points are as follows:
wsdl2java
option generates Java standard JAX-WS web service code with types and methods derived from the WSDL.InvoicePortType_InvoicePort_Server.java
, you can see that the service is published using JDK's default HTTP server provided by the javax.xml.ws.Endpoint
class. The static Endpoint.publish(…)
binds our generated service implementation (InvoicePortImpl.java
) to the endpoint address so that invoice requests are handled by our getInvoice(…)
method.wsdlLocation
parameter supplied in step 4 sets an attribute of the @javax.jws.WebService
annotation in the class InvoicePortImpl.java
.InvoicePortType_InvoicePort_Server.java
:String address = "http://localhost:9001/ws/invoice/v1";
Thread.sleep(5 * 60 * 1000);
If the generated web service stub is to be used as the basis for on-going service development, then managing the generation, build, and deploy cycle externally to SoapUI using a build framework such as Ant, Maven, or Gradle will probably be a better option. To help with this, Apache CXF has a good Maven plugin to provide similar code generation; refer to http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html.
For those who want a quick and high-level way to generate a working web service for testing purposes, I would expect SoapUI's excellent mocking features to be a more convenient option than code generation in many cases (See Chapter 3, Developing and Deploying Dynamic REST and SOAP Mocks).
The SOAP web service stub journey will be continued in the next recipe when we add simple SoapUI tests and a basic implementation to pass them.
Change the font size
Change margin width
Change background colour