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
- <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>
<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
- 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";
- }
- }
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
- 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;
- }
- }
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
- <?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>
<?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
- <wss:binding url="/greeting">
- <wss:service>
- <ws:service bean="#myJaxWSWebServiceBean"/>
- </wss:service>
- </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
- <?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>
<?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