Tuesday, January 5, 2016

JAX-WS + Spring integration example

Project Folder

See the final project folder structure.
jaxws-spring-folder-structure

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 examplehttp://localhost:8080/WebServicesExample/hello?wsdl
jaxws-spring-demo

No comments:

Post a Comment