JAX-WS + Spring integration example
Here’s a guide to show you how to integrate Spring with JAX-WS, as mention in this link : http://jax-ws-commons.java.net/spring/. Upon finishing this tutorial, you will create a simple HelloWorld web service (JAX-WS), and DI a bean into the web service via Spring.
1. Project Folder
See the final project folder structure.

2. Project Dependencies
Use Maven to get all the library dependencies. The key to integrate Spring with JAX-WS is via jaxws-spring.jar.
File : pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong</groupId> <artifactId>WebServicesExample</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>WebServicesExample Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>java.net</id> <url>http://download.java.net/maven/2</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <!-- JAX-WS --> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2.3</version> </dependency> <!-- Library from java.net, integrate Spring with JAX-WS --> <dependency> <groupId>org.jvnet.jax-ws-commons.spring</groupId> <artifactId>jaxws-spring</artifactId> <version>1.8</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </exclusion> <exclusion> <groupId>com.sun.xml.stream.buffer</groupId> <artifactId>streambuffer</artifactId> </exclusion> <exclusion> <groupId>org.jvnet.staxex</groupId> <artifactId>stax-ex</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <finalName>web services</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
The jaxws-spring’s pom.xml has a lot of unnecessary dependencies, you may need to exclude it via </exclusions> tag.
3. JAX-WS Hello World
A simple JAX-WS example, and dependency inject (DI) “HelloWorldBo” via Spring.
File : HelloWorldWS.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; import com.mkyong.bo.HelloWorldBo; @WebService public class HelloWorldWS{ //DI via Spring HelloWorldBo helloWorldBo; @WebMethod(exclude=true) public void setHelloWorldBo(HelloWorldBo helloWorldBo) { this.helloWorldBo = helloWorldBo; } @WebMethod(operationName="getHelloWorld") public String getHelloWorld() { return helloWorldBo.getHelloWorld(); } }
4. Beans
Here’s the HelloWorldBo class, with a getHelloWorld() method to return a simple string.
File : HelloWorldBo.java
package com.mkyong.bo; public interface HelloWorldBo{ String getHelloWorld(); }
File : HelloWorldBoImpl.java
package com.mkyong.bo.impl; import com.mkyong.bo.HelloWorldBo; public class HelloWorldBoImpl implements HelloWorldBo{ public String getHelloWorld(){ return "JAX-WS + Spring!"; } }
5. Spring Beans Configuration
Spring beans configuration file to bind URL pattern “/hello” to “com.mkyong.ws.HelloWorldWS” web service class.
File : applicationContext.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" xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd" > <wss:binding url="/hello"> <wss:service> <ws:service bean="#helloWs"/> </wss:service> </wss:binding> <!-- Web service methods --> <bean id="helloWs" class="com.mkyong.ws.HelloWorldWS"> <property name="helloWorldBo" ref="HelloWorldBo" /> </bean> <bean id="HelloWorldBo" class="com.mkyong.bo.impl.HelloWorldBoImpl" /> </beans>
With this jaxws-spring integration mechanism, the sun-jaxws.xml file is no longer required.
6. web.xml
In web.xml, declares “com.sun.xml.ws.transport.http.servlet.WSSpringServlet“, and link it to “/hello“.
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring + JAX-WS</display-name> <servlet> <servlet-name>jaxws-servlet</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSSpringServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>jaxws-servlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!-- Register Spring Listener --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
7. Demo
Start the project, and access the deployed web service via URL “/hello“, for example http://localhost:8080/WebServicesExample/hello?wsdl

Reference
- JAX-WS + Java Web Application Integration Example
- http://jax-ws-commons.java.net/spring/
- http://weblogs.java.net/blog/kohsuke/archive/2007/01/spring_support.html

I’d like to warning about URLs for schema in spring configuration.
These now are:
http://jax-ws.java.net/spring/servlet.xsd
and
http://jax-ws.java.net/spring/core.xsd
(but namespace is still http://jax-ws.dev.java.net/spring/core and
http://jax-ws.dev.java.net/spring/servlet)
Hi,
I tried to compile but getting error in spring config file for wss binding :
Error :
Multiple annotations found at this line:
- schema_reference.4: Failed to read schema document ‘http://jax-ws.dev.java.net/spring/servlet.xsd’, because 1) could not
find the document; 2) the document could not be read; 3) the root element of the document is not .
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ‘wss:binding’.
I don’t know if this is a bug or not.
http://localhost:8080/WebServicesExample/hello?wsdl
Won’t give out the result as mentioned. I have to modify the pom.xml to be like this:
WebServicesExample
Or else it just won’t find the wsdl.
Hi sir…
I am created different modules using Struts2 + Spring3.0 + Hibernate – JPA. Now my question is that How can I connect each independent module using web Service especially Spring Web Service.
Can Any one is helping me on this???
Thankssssssssss in Advance.
How would i create client to call this web service.?
thanks
In pom.xml getting the below error
cvc-complex-type.2.4.c: The matching wildcards is strict, but no declaration can be found for element ‘wss:binding’
at line:
What is the problem for this one?
That’s because the url of xsd changed since mkyong wrote this tutorial.
You can change on xsd:schemaLocation:
http://jax-ws.dev.java.net/spring/core http://jax-ws.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet http://jax-ws.java.net/spring/servlet.xsd
So, I’ve got it all working, and running. But what url do I enter in the browser so that the method :
HelloWorldBoImpl::getHelloWorld() is called, which should return
“JAX-WS + Spring!”
?
Hello!
Could you do a Web Services integration via SPRING + Apache CXF? I mean Servicemix +Spring
Works like a charm … Thx a lot!
this works only for such simple example.
if you try to do something further – lots or errors
nice work ..
I’m getting a null pointer when i try to create a soapUI project to this webservice.
Have you any suggestion to this problem?
I want jax ws +spring +hibernate example with database integration any help plz.
No it is not possible.
I want jax ws +spring +hibernate example with database integration any one help me plz…..
How to use my own creation of wsdl file in Apachi CXF web service?
sorry to include this…. am running apache tomcat 7.0.30
No exception when running but when i goto http://localhost:8080/WebServicesExample/hello?wsdl its giving me 404 not found error… help me rectify this problem
I am trying to get things working on JBoss. Parsing applicationContext.xml keeps failing. I pass in the latest Spring jar files into JBoss via -classpath but it seems like it’s using old jars. I have read that you can have problems parsing applicationContext.xml with old Spring jar files. I have tried EVERYTHING to get past this parse error. Nothing is working. Has anybody else got this working on JBoss?
14:44:27,408 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-
host].[/kkkkkk]] (MSC service thread 1-7) Exception sending context initialized
event to listener instance of class org.springframework.web.context.ContextLoade
rListener: org.springframework.beans.factory.BeanDefinitionStoreException: Line
11 in XML document from class path resource [applicationContext.xml] is invalid;
nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber
: 57; Document root element “beans”, must match DOCTYPE root “null”.
SEVERE: Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 28 in XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘property’. One of ‘{“http://www.springframework.org/schema/beans”:import, “http://www.springframework.org/schema/beans”:alias, “http://www.springframework.org/schema/beans”:bean, WC[##other:"http://www.springframework.org/schema/beans"]}’ is expected.
A god damm trouble do deal with dependencies, and jboss, and jdk 6 and everything
but after a lot of trouble, a great solution for web services
thanks
Hi,
I am attempting to deploy a JAX-WS service like this using WSSpringServlet and instead of wiring my beans in using XML I am attempting to use @Autowired with no success. Is there any reason you can think of this shouldn’t work, or do I need to load something else from spring to make autowiring work?
Thanks
Hi mkyong , I have working fine these with Tomcat. Do you know if JAX-WS + Spring work also with weblogic 10.
I am trying but I could get it works.
Any help/link is welcome.
Cheers.
hello
thx for the tutorial – checked it out on apache tomcat but get this exception:
java.lang.ClassCastException: com.sun.xml.ws.transport.http.servlet.WSSpringServlet cannot be cast to javax.servlet.Servlet
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1116)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:680)
any idea?
thx
cheers
c
Hi Chris, I am also facing the same problem “java.lang.ClassCastException: com.sun.xml.ws.transport.http.servlet.WSSpringServlet cannot be cast to javax.servlet.Servlet”. Have you find the solution?
I just add below in exclusion list and it worked:-
javax.servlet
servlet-api
Excellent tutorial!!
MK, when you have some free time, could you please expand on this example by providing another web application that consumes the data as a client?
This would really help give the bigger picture.
Thanks much!
you are a pioneer in this industry!
Hi , when I am working on this example, while writing the applicationContext.xml file
it is giving error as
Multiple annotations found at this line:
- Start tag of element
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element
‘wss:binding’.
I have the same problem and i can’t find the solution. Many people have the same but nobody solved it.
It seems to be something wrong with the url of the schemas, but i couldn’t find the new ones.
Make sure you have the xbean-spring-2.8.jar in the lib. I did this with Ant (can’t use maven due to firewall block to repo), that’s the error I got when I don’t have this jar file. Very misleading error as I thought it’s schema related.
Works perfectly on tomcat. I was trying to deploy it on Websphere but got an error. It can’t start the application. Can you please direct me how to change this project so that I can deploy in websphere.
A client sample to call this will be of great help too. Thanks in advance gurus.
A very good example. Gives all the startup information to build JAXWS-Spring webservices. And It works perfectly. :)
Hi,
I did an mvn install for the above example, and then I just deployed the war file obtained to glassfish. But when i tried to access the wsdl url u mention(http://localhost:8080/WebServicesExample/hello?wsdl), I get an error 404 page not found.Can anyone tell me why this might be happening?
Hi,
I’ve just landed to this great source of help and I’ve also experienced your 404 error. I solved it by poiting the browser to “http://localhost:8084/hello?wsdl”. Take a look at “http://localhost:8084/hello”, and you’ll see the reference to the wsld.
P.D.: The application was deployed in a Tomcat 7.0.22.
regards
Thanks.Most of your Examples very helpful.But this i got this exception.
I believe you are getting a class not found exception. Easy to fix.
Besides that, this is a very good tutorial.
Thanks Harsha.I added some extra libraries and it’s work now.
Yes this is very good example.
Hi Harsha it’s me again.
This example work fine with Apache Tomcat and Glassfish servers.
I have run this on Jboss 6 server.
But it’s not working with jboss 6.
Why this is not working with jboss 6?
Thanks a lot for you example, now I’m trying to make it more secure with wss, can you explain more about it please??.. thanks
Sorry, no familiar with wss. Implemented authentication with SSL before, please share your find out on wss in future :)
Just wanted to share my experience with this example.
I downloaded zip file, opened in Netbeans 6.9.1, compiled successfully, deployed through Tomcat 6 manager page. The final name for the war file in the pom.xml is “web service” so the war file is “web service.war” and when tomcat deploys it get deployed under the name “web services” instead of “WebServicesExample” as the demo link has. I changed the name of the finalName parameter in the pom.xml file to be “WebServicesExample”, recompiled and redeployed. The demo link worked after that.