These are the steps to create a SOAP web service client from a wsdl file using the CXF codegen plugin:
1) Create a project
Create a new Maven project in eclipse using the quickstart archetype.
2) Import the wsdl file into the project
Import the wsdl file into directory src/main/resources.
3) Add plugins and dependencies to the pom.xml file
Add the maven-compiler-plugin and the cxf-codegen-plugin as follows. The codegen configuration includes the directory for generated clases and the location of the wsdl file.
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <cxf.version>3.0.2</cxf.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf.version}</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <sourceRoot>${basedir}/src/generated</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/resources/CustomerOrders.wsdl</wsdl> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin> </plugins> <finalName>wsdlclient</finalName> </build>
Add dependencies to the pom.xml as shown next:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency>
4) Generate Classes with CXF codegen
Right-click on the project and select Run As > Maven generate-sources. Several java files will be added to the src/generated directory, including request and response objects, a port type interface and a Service object. The service object will handle the communication with the web service.
5) Create a class to handle interaction with the web service
Create a class that will interact with the web service using the classes generated in step 4. Here’s an example:
public class SoapClientApp { public static void main( String[] args ) { try { URL url = new URL("http://localhost:8080/04wsdlfirstws/services/customerOrders?wsdl"); CustomerOrdersService service = new CustomerOrdersService(url); CustomerOrdersPortType port = service.getCustomerOrdersPort(); GetOrdersRequest request = new GetOrdersRequest(); request.setCustomerId(BigInteger.valueOf(1)); GetOrdersResponse response = port.getOrders(request); List<Order> orders = response.getOrder(); for (Order order : orders) { List<Product> products = order.getProduct(); for (Product product : products) { String description = product.getDescription(); BigInteger quantity = product.getQuantity(); System.out.println("Product description: " + description); System.out.println("Product quantity: " + quantity); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); }; } }