Simple SOAP Client using XMLBeans


In this article we discuss the construction of a client API to the Soap Server constructed in the prervious article: Simple Spring-WS Application [with JPA and Jersey REST].    The product of this client project will be a jar file that can be included as a pom depenency (Maven) to provide easy access to the web services of that previous article. In the next article we will utilizes this client jar file to connect to the web service where we will see the benfit both of the sepration of concerns as well as the ease in which we can integrate through Maven.

Implementing the SOAP Client using XMLBeans

From the last article we created a service for a company directories which allowed the following functionality:

  • get all persons
  • get all companies
  • get persons in a company
  • update person
  • create person
  • delete person

In our client project we wish to make a request on a web service which will carry out the following basic behaviour:

  1. dispatch a request to the web service
  2. recieve a response from the web service

On the SOAP Server side we utilized XJC to generate from the XSD the classes that are used to serialize our entity objects in and out of SOAP messages.  These intermediaries act as a sort of data transfer object which come with their own bean mapper: mapping the entity objects into the XSD generated schema objects.

On the client side we are utilizing XMLBeans services to provide the marhalling of our objects.  XMLBeans xml beans is a technology that enables access to XML through binding it to Java types.  Much of the work is automated by the xmlbeans-maven-plugin in the POM file.  We include the general-client.jar that we created in the previous article.  This jar contains the XSD file used by the XMLBean plugin to generate the schema classes that we will be populating with our SchemaUtil.  Below are the related additions to the POM file.

			<xsdJar implementation="java.lang.String">com.persistent:general-client</xsdJar>

To send the message to add a person we will be serializing a person object from the client side.  The following sequence diagram describes what is happening in process of dispatching an add person request and response:


Sequence Diagram


To Connect with the Web Service we utilize WebServiceGatewaySupport from Spring.  The WebServiceGatewaySupport is a convenient super class for applications that need Web services.  We are utilizing it in a very generic way with our single endpoint.

The coding required to implement the client is minimal.  As illustrated in the sequence diagram above here is the code that implements the add person function:

public Person createPerson(final Person person) throws DatatypeConfigurationException {"Entered SoapCompanyDirectory createPerson");
		Enum add = Operation.CREATE;
		PersonRequestDocument personRequestDocument = PersonRequestDocument.Factory.newInstance();
		PersonRequest personRequest = personRequestDocument.addNewPersonRequest();
		personRequest.setOperation(add);"Before null assert");
		assert(person!=null);"After null assert");
		PersonResponseDocument personResponseDocument = (PersonResponseDocument)getWebServiceTemplate().marshalSendAndReceive(personRequest);
		PersonResponseDocument.PersonResponse personResponse = personResponseDocument.getPersonResponse();
		return SchemaConversionUtil.toEntityType(personResponse.getPerson());

The class SoapCompanyDirectory extends the WebServiceGatewaySupport class to provide the functions required to support the methods available on the SOAP server.  We utilize this class in CompanyDirectoryImpl.  This class implements the CompanyDirectory interface that we developed in the service project.  This now provides us with the option of inject, in a Spring application, either a local or remote SOAP CompanyDirectory service.

The following is the class diagram:

Client Class Diagram



Spring ApplicationContext

The Spring ApplicationContext file pulls the elements together setting the messageFactory used by the SoapCompanyDirectory ( derived from  WebServiceGatewaySupport) to SaajSoapMessageFactory.  SaajSoapMessageFactory provides the SSAJ-specific implementation implemention of the WebServiceMessageFactory utilized by SoapCompanyDirectory.  Additionally the marshalling of the objects from schema to entity class representation is provided throught the XmlBeanMarshaller

<bean id="messagFactory" class=""/>

<bean id="abstractClient" abstract="true">
    <constructor-arg ref="messagFactory"/>
    <property name="destinationProvider">
        <bean class="">
            <property name="wsdl" value="http://localhost:8080/general/companies/companySearch.wsdl"/>

<bean id="marshaller" class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller"/>

<bean id="soapCompanyDirectory" parent="abstractClient" class="">
    <property name="marshaller" ref="marshaller"/>
    <property name="unmarshaller" ref="marshaller"/>

Installation Instructions

  1. On down loading the source code which is in a zip filed called: uncompress it
  2. To build type: mvn install -Dmaven.test.skip=true
  3. Start the SOAP server as described in the previous article.
  4. run the tests to validate the installation and build of the SOAP Client with: mvn test

The Source Code

Follow this link to down load the source code.

Update code (Addition of REST Client code see: Jersey REST Client) October 29, 2009

About The Author

David Sells is a computer consultant in Toronto, Ontario who specializing in Java Enterprise Development. He has  provided innovative solutions for clients including: IBM, Goldman Sachs, Merrill Lynch and Deutsche Bank.

He also holds the following certifications:

  • Sun Certified Enterprise Architect for the Java Platform, Enterprise Edition 5 (2009)
  • Sun Certified Web Component Developer
  • Sun Certified Java Programmer.



5 comments to Simple SOAP Client using XMLBeans

Leave a Reply to Jersey REST Client « Persistent Designs Inc.




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>