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.


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:
Download the project
You can access the project site at Google's Project Hosting at

You can download the project as a Maven build. Look for the 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! 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


  1. Is any one getting the following error:

    Could not access remote service at [null]; nested exception is Unsupported endpoint address: /

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

  3. How can I modify this configuration in order to load the webservice endpoint address from DB ? My problem is that test environment address is different from production.

  4. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

  5. i really likes your blog and You have shared the whole concept really well. and Very beautifully written,
    soulful read! thanks for sharing.
    gclub casino
    gclub casino

  6. Finding the time and actual effort to create a superb article like this is great thing. I’ll learn many new stuff right here! Good luck for the next post buddy..

    Best Industrial Training in Noida
    Best Industrial Training in Noida

  7. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing.

    Best BCA Colleges in Noida

  8. Thank you very much for posting and sharing this great Blog And Good Information.carry on your posting.

    Best Linux training in Noida
    Linux Training Institute in Noida

  9. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    core java training in Electronic City

    Hibernate Training in electronic city

    spring training in electronic city

    java j2ee training in electronic city

  10. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.
    Also great with all of the valuable information you have Keep up the good work you are doing well.
    Java training in Chennai

    Java training in Bangalore

    Java online training

    Java training in Pune