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:
- Download the wsclient-sample
- Download the mailwebapp-sample
- 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).
