ICE09 . playing with java, scala, groovy and spring .

Playing with Spring

  • Archives

  • Advertisements

Consuming Services – the Spring Web Service Client

Posted by ice09 on August 25, 2008

There is a downloadable zipped Eclipse project at the end of this post.

Having created a sample (mail) web service some posts ago, we would like to have a client consuming this service.

With Spring Web Services, this can easily be done by using the ubiquitous Template Pattern and, in this special case, the WebServiceTemplate . As the project structure can be downloaded, only the two important snippets are shown here:

The application-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi=""

    <bean id="mailclient" class="de.mail.MailClient">
    	<property name="webServiceTemplate" ref="webServiceTemplate"/>

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

    <bean id="webServiceTemplate" class="">
        <constructor-arg ref="messageFactory"/>
        <property name="marshaller" ref="marshaller"/>
        <property name="unmarshaller" ref="marshaller"/>
        <property name="defaultUri" value="http://localhost:8080/mailservice"/>

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
		<property name="classesToBeBound">


and the actual Web Service client:

package de.mail;

import org.mail.mailschema.MailRequest;
import org.mail.mailschema.MailResponse;

public class MailClient {

	private WebServiceTemplate webServiceTemplate;

	public void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
		this.webServiceTemplate = webServiceTemplate;

	public boolean send(String user, String password, String from, String to, String subject, String content) {
		MailRequest request = new MailRequest();
		return ((MailResponse)webServiceTemplate.marshalSendAndReceive(request)).isResult();


The servlet sample is not shown, since it is just for demonstration and not a nice piece of code. However, it exemplifies some Spring antipatterns, because DI is not used and the servlet is not Springified at all. But it’s only a sample showing how to use the Web Service client.
By deploying the servlet (or using mvn jetty:run again), a mail can be send by calling http://server/mailwebapp/Mailer?user=USER&password=PASSWORD&to=RECIPIENT@BLA.DE&from=me&subject=SUBJECT&content=MAILTEXT
But the actual code could be much nicer (also, it is definitely not safe – just a counter and a whitelist is added to prevent immediate abuse :))

Sample Mail Web Service client project:


  1. Download the wsclient-sample
  2. Download the mailwebapp-sample
  3. Download the maildatabinding

If correctly imported as Eclipse projects, the dependencies should be automatically resolved.
The mailwebapp can be deployed as a usual Web application which uses the Web Service client to call the Mail Web Service, which must be already deployed (compare the Spring Web Service post).


7 Responses to “Consuming Services – the Spring Web Service Client”

  1. John Wang said

    Thanks for providing very useful information here. The only thing I’m not sure is: what org.mail.mailschema.MailRequest and org.mail.mailschema.MailResponse looks like.
    Do you create them manually or by xjc? what’s your xsd file? I downloaded your samples above, but can’t find them.

  2. ice09 said

    Hi John,

    the classes are automatically generated (and compiled) by the maven-jaxb-plugin (which presumably uses xjc).
    The XSD is in (Link maildatabinding): \simpledatabinding\src\main\resources\mail.xsd.
    The code generation should work if you enable the Maven-Eclipse-Plugin as mentioned in the first posts.
    If Eclipse is used with the Maven plugin, everything should be built automatically after importing the projects into Eclipse (and running mvn install).



  3. Aygun said

    link is broken( can you send me the code? thanx in advance

    • ice09 said

      Hi Aygun,

      sorry, I am evaluating this and try moving the sources to some open available space. However, there is not much more about it than the posted sources.
      However, I will try to move by the weekend.



  4. ice09 said

    Links should work again.

  5. kolla said

    Hey I tried this example as it is but changing the different web service url which is running in my local with my Input classes. I am getting the below exception:

    INFO: Creating JAXBContext with classes to be bound [class com.papajohns.selfserviceportal.eFormBusinessService.generated.RequestInformation,class com.papajohns.selfserviceportal.eFormBusinessService.generated.ResponseInformation]
    Exception in thread “main” Cannot find dispatch method for {}requestInformation
    at com.papajohns.selfserviceportal.client.EFormServiceClientImpl.sendMessage(
    at com.papajohns.selfserviceportal.client.EFormServiceClientImpl.main(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at com.intellij.rt.execution.application.AppMain.main(

    Please suggest.


  6. Shawna said

    “Consuming Services – the Spring Web Service Client
    ICE09 . playing with java, scala, groovy and spring .
    ” was indeed definitely compelling and educational! In todays universe that is really difficult to execute.
    Thanks, Ingrid

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: