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.

jaxws-spring-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>
Note
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>
Note
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

jaxws-spring-demo

Reference

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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
59 Comment threads
19 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
65 Comment authors
Parth Ajmanibvn13dmmRajaAbhishek Recent comment authors
newest oldest most voted
Guillermo Díaz S.
Guest
Guillermo Díaz S.

Mkyong, do you have a example where you use spring 4 annotation config?

Hasto
Guest
Hasto

i found solving problem xsd from this : http://stackoverflow.com/questions/14741729/jax-ws-schema-http-jax-ws-dev-java-net-spring-servlet-xsd-not-able-to-be-found

or u can get my part code .xml this :

but i still have error, i cant access : http://localhost:8080/WebServicesExample/hello?wsdl
404 Not Found : Invalid Request
anyone can help me?

sakshi
Guest
sakshi

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?

viswa
Guest
viswa

Hi , when I am working on this example, while writing the applicationContext.xml file it is giving error as <?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> 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’.

kray
Guest
kray

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.

Javier
Guest
Javier

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

Sasika
Guest
Sasika

Thanks.Most of your Examples very helpful.But this i got this exception. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sun.xml.ws.transport.http.servlet.SpringBinding#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot create inner bean '(inner bean)' of type [org.jvnet.jax_ws_commons.spring.SpringService] while setting bean property 'service'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': FactoryBean threw exception on object creation; nested exception is java.lang.NoClassDefFoundError: com/sun/xml/bind/api/JAXBRIContext at Caused by: java.lang.NoClassDefFoundError: com/sun/xml/bind/api/JAXBRIContext at com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:203) at com.sun.xml.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:343) at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:205) at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:513) at org.jvnet.jax_ws_commons.spring.SpringService.getObject(SpringService.java:333) at org.jvnet.jax_ws_commons.spring.SpringService.getObject(SpringService.java:45) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142) ... 53 more Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.api.JAXBRIContext at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361) ... 60 more

Vijay
Guest
Vijay

A very good example. Gives all the startup information to build JAXWS-Spring webservices. And It works perfectly. 🙂

june
Guest
june

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.

Zeus
Guest
Zeus

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!

chris
Guest
chris

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

Juan
Guest
Juan

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.

Michael
Guest
Michael

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

Leonardo
Guest
Leonardo

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

rushit
Guest
rushit

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.

Gilbert Engle
Guest
Gilbert Engle

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]… Read more »

Nandhu
Guest
Nandhu

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

Nandhu
Guest
Nandhu

sorry to include this…. am running apache tomcat 7.0.30

Panneer
Guest
Panneer

How to use my own creation of wsdl file in Apachi CXF web service?

neha
Guest
neha

I want jax ws +spring +hibernate example with database integration any one help me plz…..

neha
Guest
neha

I want jax ws +spring +hibernate example with database integration any help plz.

john
Guest
john

I’m getting a null pointer when i try to create a soapUI project to this webservice.
Have you any suggestion to this problem?

anudeep
Guest
anudeep

nice work ..

Maxim
Guest
Maxim

this works only for such simple example.
if you try to do something further – lots or errors

Subcomandante_M
Guest
Subcomandante_M

Works like a charm … Thx a lot!

CHris
Guest
CHris

Hello!

Could you do a Web Services integration via SPRING + Apache CXF? I mean Servicemix +Spring

will
Guest
will

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!”

?

Madav
Guest
Madav

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?

Gurditta
Guest
Gurditta

How would i create client to call this web service.?

thanks

Pradip Bhatt
Guest
Pradip Bhatt

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.