This is the fifth part in the series about SOAP Webservices with Maven. It continues after SOAP Webservices with Maven, Webservice
Now that we have a working SOAP server it is time to start thinking about the client. Since we are using Maven to generate all the server side code it would be nice if we could also generate the client side code.
Luckily, Maven has a plugin to make this possible.
Generate Client
Now that the actual WSDL file is generated it is time to generate the actual client stub code. To do this, a new project needs to be generated. This will also be a Maven project.
In this project, we start by copying the WSDL generated in the server project to the src/main/resources folder in the client project. This makes the WSDL available for code generation.
Next the needed POM changes can be made.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ractoc.coding.tutorials.soap</groupId> <artifactId>client</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>1.10</version> <executions> <execution> <id>wsimport-calculator</id> <goals> <goal>wsimport</goal> </goals> <phase>generate-sources</phase> <configuration> <wsdlUrls> <wsdlUrl>http://localhost:8080/soap-0.0.1-SNAPSHOT/calculator?wsdl</wsdlUrl> </wsdlUrls> <sourceDestDir>target/generated</sourceDestDir> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
In this POM there is just one plugin. This is the jaxws-maven-plugin. This plugin is responsible for generating the client code based on the supplied WSDL. In the configuration part of the plugin the wsdlUrl points to the location of the WSDL file. In this case there is just one. The sourceDestDir contains the location of the generated sources, as usual these go into the target/generated folder.
Client runner
Now that the client stub has been created, this stub can be used in the actual SOAP client.
public class Client { public static void main(String[] args) throws MalformedURLException { CalculatorImplService service = new CalculatorImplService(); CalculatorImpl calculator = service.getCalculatorImplPort(); AddValuesRequest request = new AddValuesRequest(); request.setA(new BigDecimal(3)); request.setB(new BigDecimal(7)); AddValuesReply reply = calculator.add(request); System.out.println(reply.getTotal()); } }
In this client code the a CalculatorImplService is instantiated. This is one of the classes generated by the Maven plugin. After that, a CalculatorImpl is requested from the CalulatorImplService. This class is also generated by tghe Maven plugin. This CalculatorImpl class is the actual client representation of the Calculator webservice. As such, it is possible to call the actual server side method directly on this class. The combination of generated classes along with the jaxws libraries then takes care of the entire client-server communication.
To call the add method on the server, first a AddValuesRequest object needs to be created. This object takes the A and B parameters. Once this is done, the request object can be passed into the calculator.add method, which in turn returns an AddValuesReply object. This object contains the answer from the server, which is printed to System.out.
The result from the example here should be 10.
Conclusion
Now you have seen how to create the SOAP server in a contract first approach. You have also seen what role Maven plays in this approach. Next you have seen how the client code can be generated and used based on the server code. Using Maven like this makes it possible to automate a lot of the complex tasks of working with SOAP. This makes SOAP a lot easier to work with, and a lot less error prone.
This tutorial continuous in the next part of the series, SOAP webservices with Maven, WSDL generation