Friday, December 31, 2010

Spring MVC 3 - Accessing web services using JAX-WS

In this tutorial we will create a Spring web service client using JAX-WS. We will integrate this client with a Spring MVC 3 application and provide a form where users can submit data. Our application is a front-end for a subscription web service, which we created in the tutorial Spring WS 2 and Spring 3 MVC Integration Tutorial. It's highly recommend to read that first because that is our web service provider.

We will assume that we only know the WSDL of the web service to simulate a real client development. To generate the proxy classes, we will use the built-in wsimport tool from the JDK. To access the web service, we will use Spring's JaxWsPortProxyFactoryBean. We are not using any Spring Web Service (WS) dependencies here.

What we'll do here is follow the same exact steps as shown in the Spring WS - MVC: Implementing a Client Tutorial. And we will just highlight the difference.

Here's are the changes you need to do to use Spring's JaxWsPortProxyFactoryBean

new applicationContext.xml

old applicationContext.xml

In the applicationContext we just removed the reference to spring-ws.xml and switched to jax-ws.xml

We need to declare this new config and delete the old one.

jax-ws.xml

For a detailed description of this configuration, please check the official Spring Reference: 19. Remoting and web services using Spring

We also need to update the primary controller.

new MainController

This controller declares two mappings:
/main - for showing the subscribe page
/main/subscribe - for processing the subscription
Notice we have injected an instance of a SubscriptionPort

In the Spring WS tutorial we have injected an instance of a WebServiceTemplate instead

Inside the doSubscribe() method, we process the subscription by delegating to the subscriptionJaxProxyService

Once we get a valid response, we add it to the model:

If there are validation errors, the client will throw a SoapFaultClientException so we need to catch that:

That's it. Everything else is exactly the same as with our previous tutorial. We've managed to integrate a Spring MVC application and utilized the JaxWsPortProxyFactoryBean.

To test the client and the web service provider, you need two instances of Tomcat or Jetty : one for the web service and one for the client. Make sure to set the ports accordingly. For the provider, I chose port 8081. For the client, I chose 8080. If you change the ports, you must update the spring-ws.xml configuration of the provider and the client as well. In case you forgot, the web service provider is in my other tutorial Spring WS 2 and Spring 3 MVC Integration Tutorial

To access the Subscribe Page, use the following URL:
http://localhost:8080/spring-ws-client-jaxproxy/krams/main
Download the project
You can access the project site at Google's Project Hosting at http://code.google.com/p/spring-ws-2-0-0-rc2-tutorial/

You can download the project as a Maven build. Look for the spring-ws-client-jaxproxy.zip in the Download sections.

You can run the project directly using an embedded server via Maven.
For Tomcat: mvn tomcat:run
For Jetty: mvn jetty:run

If you want to learn more about Spring MVC and integration with other technologies, feel free to read my other tutorials in the Tutorials section.
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring MVC 3 - Accessing web services using JAX-WS ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

3 comments:

  1. Is any one getting the following error:

    Could not access remote service at [null]; nested exception is javax.xml.ws.WebServiceException: Unsupported endpoint address: /

    ReplyDelete
  2. Big Thank!, Thank you very much; Your Spring web services series tutorials helps me a lot.

    ReplyDelete