天天看點

CXF整合Spring開發WebService

剛開始學webservice時就聽說了cxf,一直沒有嘗試過,這兩天試了一下,還不錯,總結如下:

要使用cxf當然是要先去apache下載下傳cxf,下載下傳完成之後,先要配置環境變量,有以下三步:

1.打開環境變量配置視窗,點選建立,建立%CXF_HOME%變量,值為你下載下傳的cxf所在的目錄,我的是D:\tools\apache-cxf-3.1.0

2.在Path變量中新加一句%CXF_HOME%\lib,注意要和已有的path變量用;隔開

3.在CLASSPATH中新加一句%CXF_HOME%\lib\cxf-manifest.jar,這裡也要和已有的classpath變量用;隔開

好了,至此配置已經完成,接下來我們建立一個web項目,把下載下傳的cxf檔案夾中的lib檔案夾下的所有jar檔案拷貝到WEB-INF中的lib檔案夾中。下來就可以正式開發了,先看看工程目錄結構:

CXF整合Spring開發WebService
IMyServer.java

@WebService
public interface IMyServer {

    @WebResult(name="addResult")
    public int add(@WebParam(name="a")int a,@WebParam(name="b")int b);
}
      

MyServerImpl.java

@WebService(endpointInterface="cxf.lenve.test.IMyServer")
public class MyServerImpl implements IMyServer {

    @Override
    public int add(int a, int b) {
        System.out.println("服務端..."+a+"+"+b+"="+(a+b));
        return a+b;
    }

}
      

寫一個接口一個實作類就可以了,至于釋出則在配置檔案中完成即可

beans.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:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <!-- 定義webservice服務,相當于釋出 -->
    <jaxws:server id="MyServer" address="/MyServer" serviceClass="cxf.lenve.test.IMyServer">
        <!--釋出服務類  -->
        <jaxws:serviceBean>
            <bean class="cxf.lenve.test.MyServerImpl"></bean>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>      

好了,文檔已經釋出,剩下的就是配置web.xml檔案了,看:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">
     <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>WEB-INF/beans.xml</param-value>
   </context-param>

   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>
   <!--cxfservlet的作用就是解析webservice請求  -->
   <servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
   </servlet>
   <!-- 所有字首為ws的請求都被解析為webservice請求 -->
   <servlet-mapping>
   <servlet-name>cxf</servlet-name>
   <url-pattern>/ws/*</url-pattern>
   </servlet-mapping>
</web-app>
      

好了,至此一個cxf整合spring的webservice的應用編寫成功了,運作該網站,在浏覽器中檢視wsdl文檔:

CXF整合Spring開發WebService

服務端成功釋出,用戶端該怎樣通路呢?從前我們使用wsimport将wsdl導出為java檔案,現在使用cxf,可以直接使用cxf中自帶的工具wsdl2java導出wsdl檔案。

先建立一個用戶端工程,在指令行進入該工程的src檔案夾中,然後,使用如下指令導出java檔案:

CXF整合Spring開發WebService

重新整理用戶端工程,就可以看到導出的java檔案了。

CXF整合Spring開發WebService
public class Client1 {

    public static void main(String[] args) {
        IMyServer iMyServer = new IMyServerService().getIMyServerPort();
        System.out.println(iMyServer.add(3, 4));
    }
}