Using Maven to Generate Wrapped or Non-Wrapped SOAP Bindings
Join the DZone community and get the full member experience.
Join For FreeThis post will briefly document the generating of Java code for a WSDL using Maven and the jaxws wsimport plugin. It will also show the difference in the services exposed when using wrapped and non-wrapped bindings.
Below is an extract from a pom.xml to generate the Java code:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>1.10</version> <executions> <execution> <goals> <goal>wsimport</goal> </goals> <configuration> <wsdlFiles> <wsdlFile>City81SOAPService.wsdl</wsdlFile> </wsdlFiles> <bindingDirectory>${basedir}/src/wsdl</bindingDirectory> </configuration> <id>generate-sources</id> <phase>generate-sources</phase> </execution> </executions> <dependencies> ......... </dependencies> <configuration> <sourceDestDir> ${project.build.directory}/generated-sources/jaxws-wsimport </sourceDestDir> <xnocompile>true</xnocompile> <verbose>true</verbose> <extension>true</extension> <catalog>${basedir}/src/jax-ws-catalog.xml</catalog> </configuration> </plugin>For the below WSDL file, the wsimport plugin will generate the following classes:
com\city81\soap\Balance.java
com\city81\soap\City81SOAP.java
com\city81\soap\City81SOAPImplService.java
com\city81\soap\CreateCustomer.java
com\city81\soap\CreateCustomerResponse.java
com\city81\soap\CreateCustomerResponseType.java
com\city81\soap\CreateStatus.java
com\city81\soap\ObjectFactory.java
com\city81\soap\package-info.java
<?xml version='1.0' encoding='UTF-8'?> <wsdl:definitions name="City81SOAPService" targetNamespace=http://soap.city81.com/ xmlns:ns1=http://schemas.xmlsoap.org/wsdl/soap/http xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://soap.city81.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:types> <xsd:schema> <xsd:import namespace="http://soap.city81.com/" schemaLocation="City81SOAPService.xsd" /> </xsd:schema> </wsdl:types> <wsdl:message name="createCustomer"> <wsdl:part name="params" element="tns:createCustomer"></wsdl:part> </wsdl:message> <wsdl:message name="createCustomerResponse"> <wsdl:part name="params" element="tns:createCustomerResponse"></wsdl:part> </wsdl:message> <wsdl:portType name="City81SOAP"> <wsdl:operation name="createCustomer"> <wsdl:input message="tns:createCustomer"></wsdl:input> <wsdl:output message="tns:createCustomerResponse"></wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="City81SOAPImplServiceSoapBinding" type="tns:City81SOAP"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="createCustomer"> <soap:operation soapAction="http://soap.city81.com/createCustomer" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="City81SOAPImplService"> <wsdl:port binding="tns:City81SOAPImplServiceSoapBinding" name="City81SOAPImplPort"> <soap:address location=http://localhost:8080/city81-soap/soap /> </wsdl:port> </wsdl:service> </wsdl:definitions>For the above settings, the generated City81SOAP class will be as below:
@WebService(name = "City81SOAP", targetNamespace = "http://soap.city81.com/") @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE) @XmlSeeAlso({ ObjectFactory.class }) public interface City81SOAP { @WebMethod(action = "http://soap.city81.com/createCustomer") @WebResult(name = "createCustomerResponse", targetNamespace = "http://soap.city81.com/", partName = "params") public CreateCustomerResponse createCustomer(@WebParam(name = "createCustomer", targetNamespace = "http://soap.city81.com/", partName = "params") CreateCustomer params); }The binding style as can be seen from the @SOAPBinding annotation at the head of the class is BARE ie non-wrapped. The method's args and return parameters are in each case represented as a single Java object. CreateCustomer and CreateCustomerResponse.
This has happened because in the pom.xml file, there is a bindingDirectory tag which points to a folder containing a binding.xml file. This file, shown below, has an enableWrapperStyle tag and the boolean value of false.
<bindings xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="City81SOAPService.wsdl" xmlns="http://java.sun.com/xml/ns/jaxws"> <!-- Disable default wrapper style --> <enableWrapperStyle>false</enableWrapperStyle> </bindings>If the boolean was true, or if there was no bindingDirectory tag in the pom.xml file, then the default SOAP binding style would be used ie WRAPPED. This would then result in the below generated City81SOAP class:
@WebService(name = "City81SOAP", targetNamespace = "http://soap.city81.com/") @XmlSeeAlso({ ObjectFactory.class }) public interface City81SOAP { @WebMethod(action = "http://soap.city81.com/createCustomer") @RequestWrapper(localName = "createCustomer", targetNamespace = "http://soap.city81.com/", className = "com.city81.soap.CreateCustomer") @ResponseWrapper(localName = "createCustomerResponse", targetNamespace = "http://soap.city81.com/", className = "com.city81.soap.CreateCustomerResponse") public void createCustomer( @WebParam(name = "surname", targetNamespace = "") String surname, @WebParam(name = "firstName", targetNamespace = "") String firstName, @WebParam(name = "balance", targetNamespace = "") Balance balance, @WebParam(name = "customerId", targetNamespace = "", mode = WebParam.Mode.OUT) Holder<String> customerId, @WebParam(name = "status", targetNamespace = "", mode = WebParam.Mode.OUT) Holder<CreateStatus> status); }The method's args are now individual Java objects and the return parameters are each represented as Holder objects with a WebParam.Mode.OUT value denoting they are return objects. This means that return objects are set as opposed to actually being returned in the method's signature.
Another way to specify bindings other than using the binding.xml file is to embed the enableWrapperStyle as a child of the portType but if a WSDL is from a third party, then having to change it every time a new version of the WSDL is released is open to errors.
<wsdl:portType name="City81SOAPImplService"> <jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"> <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle> </jaxws:bindings> ... </wsdl:portType>Back to the generated interfaces, and these of course need to be implemented. For an interface with a binding type of BARE, the implemented class would look like below:
@WebService(targetNamespace = "http://soap.city81.com/", name = "City81SOAP", portName = "City81SOAPImplPort", serviceName = "City81SOAPImplService") @SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.BARE) public class City81SOAPImpl implements City81SOAP { @Override public CreateCustomerResponse createCustomer(CreateCustomer createCustomer) { CreateCustomerResponse createCustomerResponse = new CreateCustomerResponse(); ..... return createCustomerResponse; } }
In the case of WRAPPED binding style, the SOAPBinding annotation would include parameterStyle = SOAPBinding.ParameterStyle.WRAPPED and the createCustomer method would be as below:
public void createCustomer( String surname, String firstName, Balance balance, Holder<String> customerId, Holder<CreateStatus> status) { customerId= new Holder<String>("1"); status = new Holder<CreateStatus>(CreateStatus.CREATE_PENDING); }This post shows that there are different ways to ultimately achieve the same result.
Published at DZone with permission of Geraint Jones, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments