In our previous installment, we showed how to create Java-first web services in Spring 2.5 using Apache CXF 2.0. This time we're going to show how to consume web services, once again using Spring 2.5 and Apache CXF 2.0.
Something great about the Spring/CXF duo is that your web service clients don't know that they're web service clients. The approach is simple: as in any Spring app, you define Java interfaces for your services and inject the service implementations into the client classes that use them (such as MVC controllers). CXF allows you to generate dynamic proxies according to the same Java service contract—the proxies are just another service implementation—and hence your client classes have no idea that they're calling web services.
This article shows you what you need to do to get it to work. If you haven't already done so, please see the previous article, Web Services with Spring 2.5 and Apache CXF 2.0, to get your web services set up. That article describes a user comment service that I build upon in the current article.
First we need to set up the web service client project. Here are the various dependencies involved.
These all come from the Spring 2.5 distribution. You can use whatever Spring 2.5 libraries you need for your own app. Here I just happen to be using Spring MVC and JSTL, though neither of those is required for Spring/CXF integration.
jstl.jar (in lib/j2ee)spring.jarspring-mvc.jar (in dist/modules; the sample app uses Spring MVC)standard.jar (in lib/jakarta-taglibs; this is the JSTL reference implementation)You can get these from the CXF 2.0.4 distribution. Whether all of these are really necessary I don't know—for example I'm not sure why we need the JavaMail library—but I'm just going by what the CXF user manual says.
commons-logging-1.1.jarcxf-2.0.4-incubator.jar (this is the main CXF JAR)geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)geronimo-annotation_1.0_spec-1.1.jar (JSR 250)geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)geronimo-stax-api_1.0_spec-1.0.jargeronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)jaxb-api-2.0.jarjaxb-impl-2.0.5.jarjaxws-api-2.0.jarneethi-2.0.2.jarsaaj-api-1.3.jarsaaj-impl-1.3.jarwsdl4j-1.6.1.jarwstx-asl-3.2.1.jarXmlSchema-1.3.2.jarxml-resolver-1.2.jarIn addition to the above, you will need to add jdom-1.0.jar since
Aegis databinding uses it.
In the previous article we created three classes in the
contactus package: ContactUsService,
ContactUsServiceImpl, and Message. You will
need to JAR ContactUsService (which is a service
interface) and Message (which is a domain model class)
and add the JAR as a dependency for the client application. You do
not need to include ContactUsServiceImpl since that's the
backend for the web service; it's not part of the interface. On the
client side we will see that CXF will create a web service proxy
according to the ContactUsService interface, and it will
use Message for marshalling and unmarshalling.
With the dependencies in place, we'll now create a simple client that can view user messages using the web service we created the last time. We're not going to treat the "post user message" operation in this article though it works in exactly the same way (which is why we're not going to treat it).