webservice學習筆記(十):用CXF編寫基於spring的web service
1.編碼實現
a.server端:
-建立spring的配置檔案beans.xml,在其中配置SEI
·實體類,SEI及實現類程式碼如下:
·前面釋出webservice是用endpoint,由於這一次是基於spring,所以我們使用的是xml配置檔案,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">
<jaxws:endpoint id="criminalBegin" implementor="com.wayne.sei.impl.CriminalWsImpl"
address="/gotham">
</jaxws:endpoint>
</beans>
老版本的cxf框架,xml檔案是需要新增三個import標籤的,cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml,
由於我們是新版本所以不需要,不然會報出異常,而我們的jaxws標籤,id屬性是唯一標識,implementor屬性是全類名,
address屬性是虛擬地址
-在web.xml中,配置上CXF的一些核心元件
web.xml內容如下:
<?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>day_fif_ws_cxf_spring_web_se</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置beans.xml檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- src下面檔案引數為classpath -->
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 應用啟動的監聽器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 所有的請求都會先經過cxf框架 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
·接下來我們把我們的java web專案部署到tomcat並開始執行,執行結果如圖:
這裡我們可以看到當載入beans.xml後就建立了我們的實現類物件
·訪問url如圖:
·訪問wsdl文件路徑如圖:
我們之前在beans.xml裡面配置了address屬性,所以路徑後面是專案名+address屬性值+?wsdl
·使用eclipse的web service explorer驗證我們的wsdl:
b.client端
-生成客戶端程式碼
·這裡不再贅述直接上圖:
-建立客戶端的spring配置檔案beans-client.xml,並配置
-編寫測試類請求webservice: