JAX-WS integration with spring

In this article,
we will discuss about creating a web service using JAX-WS and we will inject a bean using spring’s popular concept called Dependency injection.

Requirement:

Create a web service which can print some message and use JAX-WS with Spring to achieve it.

Step 1

Create a new maven web project

Step 2

Update pom.xml with the below dependencies

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4.   <modelVersion>4.0.0</modelVersion>
  5.   <groupId>JAXWSSpringIntegration</groupId>
  6.   <artifactId>JAXWSSpringIntegration</artifactId>
  7.   <packaging>war</packaging>
  8.   <version>0.0.1-SNAPSHOT</version>
  9.   <name>JAXWSSpringIntegration Maven Webapp</name>
  10.   <url>http://maven.apache.org</url>
  11.        <properties>
  12.         <spring.version>4.2.0.RELEASE</spring.version>
  13.     </properties>
  14.  
  15.  <dependencies>
  16.  
  17.     <dependency>
  18.              <groupId>junit</groupId>
  19.              <artifactId>junit</artifactId>
  20.              <version>3.8.1</version>
  21.              <scope>test</scope>
  22.     </dependency>
  23.  
  24.     <dependency>
  25.         <groupId>org.springframework</groupId>
  26.             <artifactId>spring-core</artifactId>
  27.         <version>${spring.version}</version>
  28.     </dependency>
  29.  
  30.     <dependency>
  31.         <groupId>org.springframework</groupId>
  32.         <artifactId>spring-context</artifactId>
  33.         <version>${spring.version}</version>
  34.      </dependency>
  35.  
  36.      <dependency>
  37.          <groupId>org.springframework</groupId>
  38.          <artifactId>spring-web</artifactId>
  39.          <version>${spring.version}</version>
  40.      </dependency>
  41.  
  42.      <dependency>
  43.          <groupId>org.jvnet.jax-ws-commons.spring</groupId>
  44.          <artifactId>jaxws-spring</artifactId>
  45.          <version>1.9</version>
  46.      </dependency>
  47.  
  48.   </dependencies>
  49.  
  50.   <build>
  51.     <finalName>JAXWSSpringIntegration</finalName>
  52.   </build>
  53.  
  54. </project>
<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>JAXWSSpringIntegration</groupId>
  <artifactId>JAXWSSpringIntegration</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>JAXWSSpringIntegration Maven Webapp</name>
  <url>http://maven.apache.org</url>
       <properties>
		<spring.version>4.2.0.RELEASE</spring.version>
	</properties>

 <dependencies>

    <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>3.8.1</version>
             <scope>test</scope>
    </dependency>

    <dependency>
	    <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
	    <version>${spring.version}</version>
    </dependency>

    <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>${spring.version}</version>
     </dependency>

     <dependency>
	     <groupId>org.springframework</groupId>
   	     <artifactId>spring-web</artifactId>
	     <version>${spring.version}</version>
     </dependency>

     <dependency>
	     <groupId>org.jvnet.jax-ws-commons.spring</groupId>
	     <artifactId>jaxws-spring</artifactId>
	     <version>1.9</version>
     </dependency>

  </dependencies>

  <build>
    <finalName>JAXWSSpringIntegration</finalName>
  </build>

</project>


We have added dependency for JAX-WS with spring and also spring related dependencies

Step 3

Create Spring beans

  1. package com.kb.bo;
  2.  
  3. public interface GreetingBO {
  4.     public String greeting();
  5.  
  6. }
  7.  
  8. package com.kb.bo.impl;
  9.  
  10. import com.kb.bo.GreetingBO;
  11.  
  12. public class GreetingBOImpl implements GreetingBO{
  13.  
  14.     public String greeting() {
  15.         return "Welcome to Spring JAX-WS integration";
  16.     }
  17. }
package com.kb.bo;

public interface GreetingBO {
	public String greeting();

}

package com.kb.bo.impl;

import com.kb.bo.GreetingBO;

public class GreetingBOImpl implements GreetingBO{

	public String greeting() {
		return "Welcome to Spring JAX-WS integration";
	}
}


Step 4

Create a JAX-WS web service

  1. package com.kb.ws;
  2.  
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebService;
  5.  
  6. import com.kb.bo.GreetingBO;
  7.  
  8. @WebService(serviceName="MyJaxWSWebService")
  9. public class MyJaxWSWebService{
  10.  
  11.     private GreetingBO greetingBo;
  12.    
  13.     @WebMethod(operationName="greeting")
  14.     public String greeting() {
  15.         return greetingBo.greeting();
  16.     }
  17.  
  18.     @WebMethod(exclude=true)
  19.     public void setGreetingBo(GreetingBO greetingBo) {
  20.         this.greetingBo = greetingBo;
  21.     }
  22. }
package com.kb.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.kb.bo.GreetingBO;

@WebService(serviceName="MyJaxWSWebService")
public class MyJaxWSWebService{

	private GreetingBO greetingBo;
	
	@WebMethod(operationName="greeting")
	public String greeting() {
		return greetingBo.greeting();
	}

	@WebMethod(exclude=true)
	public void setGreetingBo(GreetingBO greetingBo) {
		this.greetingBo = greetingBo;
	}
}


Step 5

Create a Spring bean configuration file

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:ws="http://jax-ws.dev.java.net/spring/core"
  5.        xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8.       http://jax-ws.dev.java.net/spring/core
  9.       http://jax-ws.java.net/spring/core.xsd
  10.       http://jax-ws.dev.java.net/spring/servlet
  11.       http://jax-ws.java.net/spring/servlet.xsd">
  12.  
  13. <!-- Bind the Url pattern with the web service -->
  14.     <wss:binding url="/greeting">
  15.         <wss:service>
  16.             <ws:service bean="#myJaxWSWebServiceBean"/>
  17.         </wss:service>
  18.     </wss:binding>
  19.  
  20.     <!-- Web service bean -->
  21.     <bean id="myJaxWSWebServiceBean" class="com.kb.ws.MyJaxWSWebService">
  22.         <property name="greetingBo" ref="greetingBo" />
  23.     </bean>
  24.  
  25.     <bean id="greetingBo" class="com.kb.bo.impl.GreetingBOImpl" />
  26.  
  27. </beans>
<?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.java.net/spring/core.xsd
       http://jax-ws.dev.java.net/spring/servlet 
       http://jax-ws.java.net/spring/servlet.xsd">

<!-- Bind the Url pattern with the web service -->
    <wss:binding url="/greeting">
        <wss:service>
            <ws:service bean="#myJaxWSWebServiceBean"/>
        </wss:service>
    </wss:binding>

    <!-- Web service bean -->
    <bean id="myJaxWSWebServiceBean" class="com.kb.ws.MyJaxWSWebService">
    	<property name="greetingBo" ref="greetingBo" />
    </bean>

    <bean id="greetingBo" class="com.kb.bo.impl.GreetingBOImpl" />

</beans>


We need to bind the URL pattern to our web service in this file

We have achieved the same using below lines

  1. <wss:binding url="/greeting">
  2.         <wss:service>
  3.             <ws:service bean="#myJaxWSWebServiceBean"/>
  4.         </wss:service>
  5.     </wss:binding>
<wss:binding url="/greeting">
        <wss:service>
            <ws:service bean="#myJaxWSWebServiceBean"/>
        </wss:service>
    </wss:binding>


We have defined the URL pattern as “/greeting” and provided the web service bean “myJaxWSWebServiceBean” to bind it with this pattern.

myJaxWSWebServiceBean is pointing to our actual web service class

We have defined a bean “greetingBo” by pointing it to its implementation class

We have also injected the business object “greetingBo“ in the web service bean “myJaxWSWebServiceBean

Step 6

Web.xml configuration

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xmlns="http://java.sun.com/xml/ns/javaee"
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5.        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  6.   <display-name>JAXWS-Spring-Integration</display-name>
  7.  
  8.    <servlet>
  9.         <servlet-name>jaxws-spring-servlet</servlet-name>
  10.          <servlet-class>
  11.             com.sun.xml.ws.transport.http.servlet.WSSpringServlet
  12.           </servlet-class>
  13.    </servlet>
  14.  
  15.    <servlet-mapping>
  16.                 <servlet-name>jaxws-spring-servlet</servlet-name>
  17.                 <url-pattern>/jaxws-spring</url-pattern>
  18.    </servlet-mapping>
  19.  
  20.          <!-- Register Spring Listener -->
  21.     <listener>
  22.  
  23.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  24.     </listener>
  25. </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JAXWS-Spring-Integration</display-name>

   <servlet>
    	<servlet-name>jaxws-spring-servlet</servlet-name>
    	 <servlet-class>
    		com.sun.xml.ws.transport.http.servlet.WSSpringServlet
    	  </servlet-class>
   </servlet>

   <servlet-mapping>
                <servlet-name>jaxws-spring-servlet</servlet-name>
                <url-pattern>/jaxws-spring</url-pattern>
   </servlet-mapping>

         <!-- Register Spring Listener -->
    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app>


In this file we have mapped the URL “/jaxws-spring” to “com.sun.xml.ws.transport.http.servlet.WSSpringServlet

This will enable our web service to receive resource injection through Spring

We have also added a listener “org.springframework.web.context.ContextLoaderListener” which will load the spring application context and instantiate all the beans configured.

Step 7

Generate war file from the project Deploy the same in Tomcat

Check spring-mvc-hello-world-project for how to do it

Step 8

Start the server and access the below URL

http://localhost:8080/JAXWSSpringIntegration/ jaxws-spring

We can find the WSDL with below URL

http://localhost:8080/JAXWSSpringIntegration/ jaxws-spring?wsdl

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on

Leave a Comment

  • You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

    • Please answer this simple challenge to post your valuable comment *