WebService
• WebService經過HTTP POST方式接受客戶的請求
• WebService與客戶端之間通常使用SOAP協議傳輸XML數據
• 它自己就是爲了跨平臺或跨語言而設計的
網絡上提供的服務:
http://webxml.com.cn/html
XML 版 HTTP
SOAP 簡單對象訪問協議
SOAP做爲一個基於XML語言的協議用於在網上傳輸數據。
SOAP = 在HTTP的基礎上+XML數據。
SOAP是基於HTTP的。
SOAP的組成以下:
Envelope – 必須的部分。以XML的根元素出現。
Headers – 可選的。
Body – 必須的。在body部分,包含要執行的服務器的方法。和發送到服務器的數據。java
WSDL web服務描述語言
就是一個文檔 用於描述當前服務的一些信息
服務名稱 service 標籤的name
服務的發佈地址 address 標籤的location
服務提供懂得方法 operatioin 標籤的name
方法的參數類型
方法的返回值類型c++
發佈一個WebService服務
第一步:建立一個Java項目
第二步:建立一個類,加入Webservice註解
第三步:提供一個方法sayHello
第四步:在main方法中調用jdk提供的發佈服務的方法
第五步:訪問服務的wsdl文檔(服務的發佈地址+?wsdl)http://172.29.20.5:8080/hello?wsdlweb
1 package webservice; 2 3 import javax.jws.WebService; 4 import javax.xml.ws.Endpoint; 5 6 @WebService 7 public class HolleWebService { 8 public String sayHello(String name, int i) { 9 System.out.println("服務端的sayHello方法被調用了。。。。"); 10 return "helle" + name; 11 } 12 13 public static void main(String[] args) { 14 String address = "http://172.29.20.5:8080/hello"; 15 Object implementor = new HolleWebService(); 16 Endpoint.publish(address, implementor); 17 } 18 }
jdk中wsimport命令使用
做用:解析wsdl文件,生成客戶端本地代碼
客戶端調用
使用wsimport命令解析wsdl文件生成本地代碼
經過本地代碼建立一個代理對象
經過代理對象實現遠程調用spring
package webservice; public class App { public static void main(String[] args) { HolleWebServiceService holleWebServiceService = new HolleWebServiceService(); HolleWebService proxy = holleWebServiceService.getHolleWebServicePort(); String sayHello = proxy.sayHello("stevezong", 1); System.out.println(sayHello); } }
CXFapache
支持多種協議:
• SOAP1.1,1.2
• XML/HTTP
• CORBA(Common Object Request Broker Architecture公共對象請求代理體系結構,早期語言使用的WS。C,c++,C#)
• 並能夠與Spring進行快速無縫的整合
• 靈活的部署:能夠運行在Tomcat,Jboss,Jetty(內置),IBMWS,BeaWL上面。
CXF 服務端 開發 spring + cxf
第一步:建立動態web項目
第二步:導入CXF相關jar包
第三步:在web.xml中配置CXF框架提供的一個Servlet
第四步:在類路徑下提供cxf.xml
第五步:開發一個接口和實現類
第六步:在cxf.xml中註冊服務
2
maven服務器
maven <cxf.version>3.0.1</cxf.version> <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <!-- Jetty is needed if you're are not using the CXFServlet --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency>
3網絡
wel <?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_2_5.xsd" version="2.5"> <display-name>cxf</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> <!-- <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <param-name>config-location</param-name> <param-value>classpath:spring-cxf.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/service/*.do</url-pattern> </servlet-mapping> --> <!-- 配置CXF框架提供的Servlet --> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <!-- 經過初始化參數指定CXF框架的配置文件位置 --> <init-param> <param-name>config-location</param-name> <param-value>classpath:cxf.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping> </web-app>
4
cxfapp
<?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:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 引入CXF Bean定義以下,早期的版本中使用 --> <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="helloService" class="com.stevezong.cxf.HelloServiceImpl"></bean> <jaxws:server id="myService" address="/cfxService"> <jaxws:serviceBean> <ref bean="helloService" /> </jaxws:serviceBean> </jaxws:server> </beans>
5
java框架
package com.stevezong.cxf; import javax.jws.WebService; @WebService public interface HelloService { public String sayHello(String name); }
package com.stevezong.cxf; public class HelloServiceImpl implements HelloService { public String sayHello(String name) { System.out.println("cxf的sayHello"); return name + " name "; } }
CXF 客戶端
方式一:使用jdk提供的wsimport命令生成本地代碼完成調用
方式二:使用CXF提供的方式(重點)
第一步:建立Java項目並導入CXF相關jar包
第二步:使用wsimport或者CXF提供wsdl2java生成本地代碼,只須要生成接口文件
第三步:將接口文件複製到項目中
第四步:提供spring配置文件,註冊客戶端代理對象
第五步:讀取spring配置文件,建立spring工廠,從工廠中獲取代理對象,實現遠程調用
1
maven
導包
2
C:\Program Files\Java\jdk1.8.0_111\bin>wsimport.exe -s \test http://127.0.0.1:8080/cxf/service/cfxService?wsdl 正在解析 WSDL... 正在生成代碼... 正在編譯代碼...
3
package com.stevezong.cxf; import javax.jws.WebService; @WebService public interface HelloService { public String sayHello(String name); }
4
<?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:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 引入CXF Bean定義以下,早期的版本中使用 --> <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" /> <jaxws:client id="myClient" address="http://127.0.0.1:8080/cxf/service/cfxService" serviceClass="cxf.HelloService"></jaxws:client> </beans>
5
package cxf; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test2 { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("cxf2.xml"); HelloService proxy = (HelloService) ctx.getBean("myClient"); System.out.println(proxy.sayHello("stevezong")); } }