WebService與CXF
————————————————————————————————————————————————————
一:Webservice
————————————————————————————————————————————————————
1:WebService是幹什麽的?有什麽用?
一言以蔽之:WebService是一種跨編程語言和跨操作系統平臺的遠程調用技術。
所謂跨編程語言和跨操作平臺,就是說服務端程序采用java編寫,客戶端程序則可以采用其他編程語言編寫,反之亦然!跨操作系統平臺則是指服務端程序和客戶端程序可以在不同的操作系統上運行。
比如,amazon,天氣預報系統,淘寶網,校內網,百度等把自己的系統服務以webservice服務的形式暴露出來,讓第三方網站和程序可以調用這些服務功能,這樣擴展了自己系統的市場占有率
從表面上看,WebService就是一個應用程序向外界暴露出一個能通過Web進行調用的API,也就是說能用編程的方法通過Web來調用這個應用程序。我們把調用這個WebService的應用程序叫做客戶端,而把提供這個WebService的應用程序叫做服務端。
從深層次看,WebService是建立可互操作的分布式應用程序的新平臺,是一個平臺,是一套標準。它定義了應用程序如何在Web上實現互操作性,你可以用任何你喜歡的語言,在任何你喜歡的平臺上寫Web service ,只要我們可以通過Web service標準對這些服務進行查詢和訪問。
2:什麽是SAOP??什麽是WSDL??
2.1 SAOP:
SAOP是一種WebService平臺技術
SOAP協議 = HTTP協議 + XML數據格式
WebService通過HTTP協議發送請求和接收結果時,發送的請求內容和結果內容都采用XML格式封裝,並增加了一些特定的HTTP消息頭,以說明HTTP消息的內容格式,這些特定的HTTP消息頭和XML內容格式就是SOAP協議。
當然除了SAOP還有其他WebService技術,XML+XSD,SOAP和WSDL就是構成WebService平臺的三大技術。
2.2 WSDL
好比我們去商店買東西,首先要知道商店裏有什麽東西可買,然後再來購買,商家的做法就是張貼廣告海報。 WebService也一樣,WebService客戶端要調用一個WebService服務,首先要有知道這個服務的地址在哪,以及這個服務裏有什麽方法可以調用,所以,WebService務器端首先要通過一個WSDL文件來說明自己家裏有啥服務可以對外調用,服務是什麽(服務中有哪些方法,方法接受的參數是什麽,返回值是什麽),服務的網絡地址用哪個url地址表示,服務通過什麽方式來調用。
WSDL(Web Services Description Language)就是這樣一個基於XML的語言,用於描述Web Service及其函數、參數和返回值。
一些最新的開發工具既能根據你的Web service生成WSDL文檔,又能導入WSDL文檔,生成調用相應WebService的代理類代碼。
WSDL文件保存在Web服務器上,通過一個url地址就可以訪問到它。客戶端要調用一個WebService服務之前,要知道該服務的WSDL文件的地址。WebService服務提供商可以通過兩種方式來暴露它的WSDL文件地址:1.註冊到UDDI服務器,以便被人查找;2.直接告訴給客戶端調用者。
3:什麽是REST
https://www.cnblogs.com/loveis715/p/4669091.html
REST 是一種軟件架構模式,只是一種風格,rest服務采用HTTP 做傳輸協議,REST 對於HTTP 的利用實現精確的資源定位。
Rest要求對資源定位更加準確,如下:
非rest方式:http://ip:port/queryUser.action?userType=student&id=001
Rest方式:http://ip:port/user/student/query/001
Rest方式表示互聯網上的資源更加準確,但是也有缺點,可能目錄的層級較多不容易理解。
Rest不再需要生成客戶端,直接獲取數據
———————————————————————————————————————————————
二:CXF
———————————————————————————————————————————————
1:CXF是什麽?有什麽用?優點
Apache CXF 是一個開源的web Services 框架,CXF 幫助您構建和開發 web Services ,它支持多種協議,支持數據格式:XML,JSON(僅在REST方式下支持)
2:CXF的基礎知識
2.1:安裝配置
官網下載,解壓
環境變量配置
———————————————————————————————————————————————
3:CXF與Spring整合發布SAOP與REST項目
———————————————————————————————————————————————
3.1 CXF+Spring整合發布SAOP協議服務
3.1.1服務端
開發步驟:
第一步:創建web項目,導入jar包,maven項目添加坐標
第二步:創建SEI接口(SEI在webservice中稱為portType,在java中稱為接口)
1 import javax.jws.WebService; 2 import javax.xml.ws.BindingType; 3 import javax.xml.ws.soap.SOAPBinding; 4 5 /** 6 * 7 * <p>Title: WeatherInterface.java</p> 8 * <p>Description:SEI接口</p> 9 */ 10 @WebService 11 @BindingType(SOAPBinding.SOAP12HTTP_BINDING) 12 public interface WeatherInterface { 13 14 public String queryWeather(String cityName); 15 16 }
第三布:創建SEI實現類
1 public class WeatherInterfaceImpl implements WeatherInterface { 2 3 @Override 4 public String queryWeather(String cityName) { 5 System.out.println("from client..."+cityName); 6 if("北京".equals(cityName)){ 7 return "冷且霾"; 8 } else { 9 return "暖且晴"; 10 } 11 } 12 13 }
第四步:配置Spring配置文件beans.xml
用<jaxws:server標簽發布服務,設置 1.服務地址; 2.設置服務接口; 3設置服務實現類
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" xmlns:jaxws="http://cxf.apache.org/jaxws" 4 xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 8 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 9 http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> 10 <!-- <jaxws:endpoint發布SOAP協議的服務 ,對Endpoint類封裝--> 11 <jaxws:endpoint address="/hello" implementor="com.xqc.ws.cxf.server.HelloWorld"/> 12 13 <!-- <jaxws:server發布SOAP協議的服務 ,對JaxWsServerFactoryBean類封裝--> 14 <jaxws:server address="/weather" serviceClass="com.xqc.ws.cxf.server.WeatherInterface"> 15 <jaxws:serviceBean> 16 <ref bean="weatherInterface"/> 17 </jaxws:serviceBean> 18 19 <!-- 配置攔截器 --> 20 <jaxws:inInterceptors> 21 <ref bean="inIntercepter"/> 22 </jaxws:inInterceptors> 23 <jaxws:outInterceptors> 24 <ref bean="outIntercepter"/> 25 </jaxws:outInterceptors> 26 </jaxws:server> 27 <!-- 配置攔截器的bean --> 28 <bean name="inIntercepter" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> 29 <bean name="outIntercepter" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> 30 31 <!-- 配置服務實現類 --> 32 <bean name="weatherInterface" class="com.xqc.ws.cxf.server.WeatherInterfaceImpl"/> 33 </beans>
第五步:配置Web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>ws_2_cxf_spring_server</display-name> 4 5 <!-- 設置spring的環境 ,加載spring配置文件 --> 6 <context-param> 7 <!--contextConfigLocation是不能修改的 --> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:beans.xml</param-value> 10 </context-param> 11 <listener> 12 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 13 </listener> 14 15 <!-- 配置CXF的Servlet --> 16 <servlet> 17 <servlet-name>CXF</servlet-name> 18 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 19 </servlet> 20 <servlet-mapping> 21 <servlet-name>CXF</servlet-name> 22 <url-pattern>/ws/*</url-pattern> 23 </servlet-mapping> 24 25 <welcome-file-list> 26 <welcome-file>index.html</welcome-file> 27 <welcome-file>index.htm</welcome-file> 28 <welcome-file>index.jsp</welcome-file> 29 <welcome-file>default.html</welcome-file> 30 <welcome-file>default.htm</welcome-file> 31 <welcome-file>default.jsp</welcome-file> 32 </welcome-file-list> 33 </web-app>
第六步:部署到tomact下,啟動tomact
第七步:測試服務
瀏覽器輸入:WSDL地址規則:http://ip:端口號/項目名稱/servlet攔截路徑/服務名稱?wsdl
第八步:Endpoint標簽發布服務
<jaxws:endpoint>標簽
添加文件
1 @WebService 2 public class HelloWorld { 3 public String sayHello(String name){ 4 return "hello,"+name; 5 } 6 7 }
在beans中添加配置
1 <!-- <jaxws:endpoint發布SOAP協議的服務 ,對Endpoint類封裝--> 2 <jaxws:endpoint address="/hello" implementor="com.xqc.ws.cxf.server.HelloWorld"/>
訪問:http://ip:端口號/項目名稱/servlet攔截路徑/ 例如:http://localhost:8080/ws_2_cxf_spring_server/ws/
3.1.2 客戶端(近寫一個javase的客戶端演示一下,客戶端可以很多)
開發步驟:
第一步:引入jar包
第二步:生成客戶端代碼
第三步:配置spring配置文件,applicationContent.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" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxws:client實現客戶端 ,對JaxWsProxyFactoryBean類封裝--> <jaxws:client id="weatherClient" address="http://127.0.0.1:8080/ws_2_cxf_spring_server/ws/weather" serviceClass="com.xqc.cxf.weather.WeatherInterface"/> </beans>
第四步:從spring上下文件獲取服務實現類
第五步:調用查詢方法,打印
1 package com.xqc.cxf.client; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.xqc.cxf.weather.WeatherInterface; 7 8 public class WeatherClient { 9 10 public static void main(String[] args) { 11 //初始化spring的上下文 12 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml"); 13 WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherClient"); 14 String weather = weatherInterface.queryWeather("保定"); 15 System.out.println(weather); 16 } 17 }
———————————————————————————————————————————————
3.2CXF+Spring整合發布REST服務
———————————————————————————————————————————————
3.2.1服務端
開發步驟:
第一步:導入jar包
第二步:創建學生pojo類,要加入@ XmlRootElement
1 package com.xqc.ws.rest.pojo; 2 3 import java.util.Date; 4 5 import javax.xml.bind.annotation.XmlRootElement; 6 7 /** 8 * 9 * <p>Title: Student.java</p> 10 * <p>Description:學生實體類</p> 11 */ 12 @XmlRootElement(name="student")//@XmlRootElement可以實現對象和XML數據之間的轉換 13 public class Student { 14 15 private long id; 16 17 private String name; 18 19 private Date birthday; 20 21 public long getId() { 22 return id; 23 } 24 25 public void setId(long id) { 26 this.id = id; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public Date getBirthday() { 38 return birthday; 39 } 40 41 public void setBirthday(Date birthday) { 42 this.birthday = birthday; 43 } 44 45 }
第三步:創建SEI接口
1 package com.xqc.ws.rest.server; 2 3 import java.util.List; 4 import javax.jws.WebService; 5 import javax.ws.rs.GET; 6 import javax.ws.rs.Path; 7 import javax.ws.rs.PathParam; 8 import javax.ws.rs.Produces; 9 import javax.ws.rs.core.MediaType; 10 11 import com.xqc.ws.rest.pojo.Student; 12 13 /** 14 * 15 * <p>Title: StudentInterface.java</p> 16 * <p>Description:學生接口</p> 17 */ 18 @WebService 19 @Path("/student")//@Path("/student")就是將請求路徑中的“/student”映射到接口上 20 public interface StudentInterface { 21 22 //查詢單個學生 23 @GET//指定請求方式,如果服務端發布的時候指定的是GET(POST),那麽客戶端訪問時必須使用GET(POST) 24 @Produces(MediaType.APPLICATION_XML)//指定服務數據類型 25 @Path("/query/{id}")//@Path("/query/{id}")就是將“/query”映射到方法上,“{id}”映射到參數上,多個參數,以“/”隔開,放到“{}”中 26 public Student query(@PathParam("id")long id); 27 28 //查詢多個學生 29 @GET//指定請求方式,如果服務端發布的時候指定的是GET(POST),那麽客戶端訪問時必須使用GET(POST) 30 @Produces("application/json;charset=utf-8")//指定服務數據類型 31 @Path("/queryList/{name}")//@Path("/queryList/{name}")就是將“/queryList”映射到方法上,“{name}”映射到參數上,多個參數,以“/”隔開,放到“{}”中 32 public List<Student> queryList(@PathParam("name")String name); 33 34 }
第四步:創建SEI實現類
1 package com.xqc.ws.rest.server; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.List; 6 7 import com.xqc.ws.rest.pojo.Student; 8 9 /** 10 * 11 * <p>Title: StudentInterfaceImpl.java</p> 12 * <p>Description:學生的實現類</p> 13 */ 14 public class StudentInterfaceImpl implements StudentInterface { 15 16 @Override 17 public Student query(long id) { 18 Student st = new Student(); 19 st.setId(110); 20 st.setName("張三"); 21 st.setBirthday(new Date()); 22 return st; 23 } 24 25 @Override 26 public List<Student> queryList(String name) { 27 28 Student st = new Student(); 29 st.setId(110); 30 st.setName("張三"); 31 st.setBirthday(new Date()); 32 33 Student st2 = new Student(); 34 st2.setId(120); 35 st2.setName("李四"); 36 st2.setBirthday(new Date()); 37 38 List<Student> list = new ArrayList<Student>(); 39 list.add(st); 40 list.add(st2); 41 return list; 42 } 43 44 }
第五步:
配置Spring配置文件,beans.xml,<jaxrs:server,設置1.服務地址;2.服務實現類
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" xmlns:jaxws="http://cxf.apache.org/jaxws" 4 xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 8 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 9 http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> 10 <!-- <jaxrs:server發布REST的服務 ,對JAXRSServerFactoryBean類封裝--> 11 <jaxrs:server address="/user"> 12 <jaxrs:serviceBeans> 13 <ref bean="studentInterface"/> 14 </jaxrs:serviceBeans> 15 </jaxrs:server> 16 17 <!-- 配置服務實現類 --> 18 <bean name="studentInterface" class="com.xqc.ws.rest.server.StudentInterfaceImpl"/> 19 </beans>
第六步:配置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>ws_2_cxf_spring_server</display-name> <!-- 設置spring的環境 --> <context-param> <!--contextConfigLocation是不能修改的 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置CXF的Servlet --> <servlet> <servlet-name>CXF</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXF</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <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> </web-app>
第七步:部署到tomcat下,啟動tomcat
第八步:測試服務
REST服務的使用說明書地址:
http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user?_wadl
http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user/student/query/110 查詢單個學生,返回XML數據
1 <student> 2 <birthday>2015-11-27T15:22:14.240+08:00</birthday> 3 <id>110</id> 4 <name>張三</name> 5 </student>
http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user//student/queryList/110?_type=json 查詢多個學生,返回JSON
{"student":[{"birthday":"2015-11-27T15:24:21.565+08:00","id":110,"name":"張三"},{"birthday":"2015-11-27T15:24:21.565+08:00","id":120,"name":"李四"}]}
3.2.2:客戶端:因為返回的直接就是數據,其實直接解析就可以,DOM4J解析
直接用新建html然後使用Ajax使用即可
———————————————————————————————————————————————
綜合案例訓練:
———————————————————————————————————————————————
WebService與CXF