基于Apache CXF實現Spring整合Web service

jopen 10年前發布 | 16K 次閱讀 WEB服務/RPC/SOA Apache CXF

一. 概述

可以在傳統的Java EE應用的基礎上添加一層Web Service層, 我們的Java EE應用就可以對外暴漏Web Service, 這樣就允許任何平臺、任何語言編寫的程序來調用這個Java EE應用


二. 步驟

d1.png

d2.png


1. 新建web工程springCXF, 并復制需要的Jar包:見上圖

2. 在web.xml中配置CXF的核心控制器: CXFServlet

    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  

        <!-- Spring核心配置文件 -->  
        <context-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/config/applicationContext.xml</param-value>  
        </context-param>  

        <!-- 加載Spring容器 -->  
        <listener>  
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
        </listener>  

        <!-- 配置CXF的核心Servlet -->  
        <servlet>  
            <servlet-name>CXFServlet</servlet-name>  
            <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
        </servlet>  

        <!-- 為CXF的核心Servlet配置URL -->  
        <servlet-mapping>  
            <servlet-name>CXFServlet</servlet-name>  
            <url-pattern>/webService/*</url-pattern>  
        </servlet-mapping>  
    </web-app>  

3. 在Spring配置文件applicationContext.xml中導入CXF提供的Schema, xml配置文件, 并使用jaxws:endpoint元素來暴露web service

    <?xml version="1.0" encoding="GBK"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:jaxws="http://cxf.apache.org/jaxws"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
        http://cxf.apache.org/jaxws   
        http://cxf.apache.org/schemas/jaxws.xsd">  

        <!-- 導入CXF為擴展Spring提供的幾個XML配置文件 -->  
        <import resource="classpath:META-INF/cxf/cxf.xml" />  
        <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  

        <!--   
            id:指定所定義的Bean在Spring容器中的唯一標識。   
            implementor:指定Web Services的實現類,或者引用容器中另一個已有的Bean實例  
            address:指定所創建的Web Service的地址,因為CXF不知道該Web應用對應的URL,  
                      也不知道Web服務器的監聽端口。因此address屬性指定的只是一個相對地址,  
                     CXF將會在運行時動態確定該Web Services的服務地址。     
        -->  
        <jaxws:endpoint id="sayHello" implementor="com.zdp.service.impl.HelloServiceImpl" address="/sayHello"></jaxws:endpoint>  
    </beans>  
4. 接口及實現類:

HelloService

    //以@WebService Annotation標注,表明該接口將對應一個Web Services  
    @WebService  
    public interface HelloService {  
        //定義一個方法,該方法將被暴露成一個Web Services操作  
        public void sayHello(String name);   
    }  
HelloServiceImpl

    @WebService(endpointInterface = "com.zdp.service.HelloService")  
    public class HelloServiceImpl implements HelloService{   
        public void sayHello(String name) {  
            System.out.println("hello " + name + ", current time is " + new Date());  
        }  
    }  
5. 啟動tomcat服務器

6. 新建一個Java工程cxf_Client, 在命令行進入該工程src目錄

輸入一下命令: wsdl2java http://localhost:9999/springCXF/webService/sayHello?wsdl

7. 寫一個測試類:

    public class MyClient {  
        public static void main(String[] args) {  
            HelloServiceImplService factory = new HelloServiceImplService();  
            HelloService helloService = factory.getHelloServiceImplPort(); // 返回一個代理  
            helloService.sayHello("zhangsan");  
        }  
    }  

 

三. 配置攔截器

其實配置攔截器就是在applicationContext.xml中增加一個配置, 具體的攔截器類請見上一篇博文: http://www.baiduhome.net/lib/view/open1406190305273.html

<?xml version="1.0" encoding="GBK"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://cxf.apache.org/jaxws   
    http://cxf.apache.org/schemas/jaxws.xsd">  

    <!-- 導入CXF為擴展Spring提供的幾個XML配置文件 -->  
    <import resource="classpath:META-INF/cxf/cxf.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  

    <!-- 攔截器 -->    
    <bean id="authInterceptor" class="com.zdp.interceptor.AuthInterceptor"/>   

    <jaxws:endpoint id="sayHello" implementor="com.zdp.service.impl.HelloServiceImpl" address="/sayHello">  
        <jaxws:inInterceptors>    
            <ref bean="authInterceptor"/>    
        </jaxws:inInterceptors>   
    </jaxws:endpoint>  
</beans>  
來自:http://blog.csdn.net/zdp072/article/details/29429257
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!