Spring+webservice(cxf框架)


1.服務端和服務端引入相關cxf的jar包:

 

(2).采用導包方式:

 

2.服務端編寫:

(1).定義接口:

 

(2).實現該接口,進行相應的業務邏輯:

(3).spring的配置文件編寫:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:cxf="http://cxf.apache.org/core"
 5     xmlns:jaxws="http://cxf.apache.org/jaxws"
 6     xmlns:jaxrs="http://cxf.apache.org/jaxrs"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://cxf.apache.org/core
11         http://cxf.apache.org/schemas/core.xsd
12         http://cxf.apache.org/jaxws
13         http://cxf.apache.org/schemas/jaxws.xsd
14         http://cxf.apache.org/jaxrs
15         http://cxf.apache.org/schemas/jaxrs.xsd
16         ">
17     <bean id="weatherService" class="cn.itcast.webservice.WeatherService"></bean>
18     <jaxws:server address="/weather">
19         <jaxws:serviceBean>
20             <ref bean="weatherService"/>
21         </jaxws:serviceBean>
22     </jaxws:server>
23     
24 </beans>

這里xsd約束記得添加上,以及注冊bean,以及jaxws:server的配置,別寫成jaxrs:server了

(4)web.xml的配置:

1).簡單版:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6     <display-name>ws2803</display-name>
 7     <welcome-file-list>
 8         <welcome-file>index.jsp</welcome-file>
 9     </welcome-file-list>
10 
11     <!-- spring監聽器 -->
12     <listener>
13         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14     </listener>
15     <!-- 配置sprnig的配置文件 -->
16     <context-param>
17         <param-name>contextConfigLocation</param-name>
18         <param-value>classpath:applicationContext.xml</param-value>
19     </context-param>
20     <!-- CXF核心控制器的配置 -->
21     <servlet>
22         <servlet-name>cxf</servlet-name>
23         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
24     </servlet>
25     <servlet-mapping>
26         <servlet-name>cxf</servlet-name>
27         <url-pattern>/ws/*</url-pattern>
28     </servlet-mapping>
29 </web-app>

配置spring的基本配置與cxf的核心控制器的配置

2).與spring的過濾器整合方式:

                圖 1

               圖 2

 

(5)部署發布:

1).如果是web項目直接服務器發布就行:

2).java工程采用main函數方式:

(6).測試是否發布成功:

 

 3.客戶端編寫:

(1).采用jdk自帶的wsimport.exe工具解析文檔說明書(上面的wsdl):

(2).復制client的src目錄,然后進入該目錄,使用wsimport命令解析成java文件

wsimport -s . http://localhost/ws2803/ws/weather?wsdl       
命令-s 解析到制定文件
命令. 指解析當當前文件

(3)spring中applicationContext.xml的配置,主要是客戶端的配置,IWeatherService為解析出來的接口

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:cxf="http://cxf.apache.org/core"
 5     xmlns:jaxws="http://cxf.apache.org/jaxws"
 6     xmlns:jaxrs="http://cxf.apache.org/jaxrs"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://cxf.apache.org/core
11         http://cxf.apache.org/schemas/core.xsd
12         http://cxf.apache.org/jaxws
13         http://cxf.apache.org/schemas/jaxws.xsd
14         http://cxf.apache.org/jaxrs
15         http://cxf.apache.org/schemas/jaxrs.xsd
16         ">
17         
18     <jaxws:client id="weatherBean" address="http://localhost/ws2803/ws/weather?wsdl" 
19         serviceClass="cn.itcast.webservice.IWeatherService">
20     </jaxws:client>
21 </beans>

 

 (4).測試:

1).引入applicationContext.xml形式

2).采用注解方式:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM