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

Playing with Spring

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="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

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

    <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>

    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <constructor-arg ref="messageFactory"/>
        <property name="marshaller" ref="marshaller"/>
        <property name="unmarshaller" ref="marshaller"/>
        <property name="defaultUri" value="http://localhost:8080/mailservice"/>
    </bean>

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
		<property name="classesToBeBound">
			<list>
				<value>org.mail.mailschema.MailRequest</value>
				<value>org.mail.mailschema.MailResponse</value>
			</list>
		</property>
	</bean>

</beans>

and the actual Web Service client:

package de.mail;

import org.mail.mailschema.MailRequest;
import org.mail.mailschema.MailResponse;
import org.springframework.ws.client.core.WebServiceTemplate;

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();
		request.setFrom(from);
		request.setTo(to);
		request.setUser(user);
		request.setPassword(password);
		request.setContent(content);
		request.setSubject(subject);
		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:

Instructions:

  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).

Advertisements

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 simpledatabinding.zip (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).

    Best,

    Alex

  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.

      Best,

      Alex

  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” org.springframework.ws.soap.client.SoapFaultClientException: Cannot find dispatch method for {}requestInformation
    at org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:37)
    at org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:735)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:563)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:501)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:350)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:344)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:336)
    at com.papajohns.selfserviceportal.client.EFormServiceClientImpl.sendMessage(EFormServiceClientImpl.java:39)
    at com.papajohns.selfserviceportal.client.EFormServiceClientImpl.main(EFormServiceClientImpl.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)

    Please suggest.

    Thanks,
    Kolla

  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:

WordPress.com Logo

You are commenting using your WordPress.com 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: