亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

XFire構建web service客戶端的五種方式

 更新時間:2017年01月22日 16:34:25   作者:toyzhou  
本篇文章主要介紹了XFire構建web service客戶端的五種方式。具有很好的參考價值,下面跟著小編一起來看下吧

這里并未涉及到JSR 181 Annotations 的相關應用,具體的三種方式如下

① 通過WSDL地址來創(chuàng)建動態(tài)客戶端
② 通過服務端提供的接口來創(chuàng)建客戶端
③ 使用Ant通過WSDL文件來生成客戶端

第一種方式:通過WSDL地址來創(chuàng)建動態(tài)客戶端

package com.jadyer.client; 
import java.net.MalformedURLException; 
import java.net.URL; 
import org.codehaus.xfire.client.Client; 
/** 
 * 通過WSDL來創(chuàng)建動態(tài)客戶端 
 * @see 此時需要在項目中引入XFire 1.2 Core Libraries和XFire 1.2 HTTP Client Libraries 
 */ 
public class ClientFromWSDL { 
 public static void main(String[] args) throws MalformedURLException, Exception { 
 Client client = new Client(new URL("http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl")); 
 Object[] results11 = client.invoke("sayHello", new Object[]{"Jadyer22"}); 
 System.out.println(results11[0]); 
 } 
} 

第二種方式:通過服務端提供的端口來創(chuàng)建客戶端

package com.jadyer.client; 
import java.net.MalformedURLException; 
import java.util.List; 
import org.codehaus.xfire.client.XFireProxyFactory; 
import org.codehaus.xfire.service.Service; 
import org.codehaus.xfire.service.binding.ObjectServiceFactory; 
import com.jadyer.model.Person; 
import com.jadyer.model.User; 
import com.jadyer.server.HelloService; 
/** 
 * 通過Web服務端提供的接口來創(chuàng)建客戶端 
 * @see 客戶端必須提供一個與服務端完全一致的接口,包名也要一致 
 * @see 在本例中,需要在客戶端(即該項目)中提供HelloService.java接口,以及Person和User兩個POJO類 
 * @see 并且此時需要在項目中引入XFire 1.2 Core Libraries和XFire 1.2 HTTP Client Libraries 
 */ 
public class ClientFromInterface { 
 public static void main(String[] args)throws MalformedURLException{ 
 //首先使用XFire的ObjectServiceFactory從HelloService接口創(chuàng)建一個服務模型serviceModel 
 //serviceModel包含服務的說明,換句話說,就是服務的元數(shù)據(jù) 
 //Create a metadata of the service 
 Service serviceModel = new ObjectServiceFactory().create(HelloService.class); 
 //訪問的地址 
 String serviceURL = "http://127.0.0.1:8080/XFire_demo/services/XFireServer"; 
 //通過查看org.codehaus.xfire.client.XFireProxyFactory源碼發(fā)現(xiàn) 
 //下面兩行代碼與這里直接new XFireProxyFactory()的作用是等效的 
 //XFire xfire = XFireFactory.newInstance().getXFire(); 
 //XFireProxyFactory factory = new XFireProxyFactory(xfire); 
 //為XFire獲得一個代理工廠對象 
 //Create a proxy for the deployed service 
 XFireProxyFactory factory = new XFireProxyFactory(); 
 //通過proxyFactory,使用服務模型serviceModel和服務端點URL(用來獲得WSDL) 
 //得到一個服務的本地代理,這個代理就是實際的客戶端 
 HelloService client = (HelloService)factory.create(serviceModel, serviceURL); 
 /** 
  * Invoke the service 
  * @see 調用服務的本地代理(即實際的客戶端)中的方法,便得到我們需要的WebServcie 
  */ 
 /*--處理簡單對象--*/ 
 String serviceResponse = client.sayHello("Jadyer11"); 
 System.out.println(serviceResponse); 
 /*--處理對象--*/ 
 User u = new User(); 
 u.setName("Jadyer99"); 
 Person pp = client.getPerson(u); 
 System.out.println(pp.getName()); 
 /*--處理List--*/ 
 List<Person> personList = client.getPersonList(24, "Jadyer88"); 
 for(Person p : personList){ 
  System.out.println(p.getName()); 
 } 
 } 
} 

這是它要用到的接口和兩個POJO類

/** 
 * Web服務提供給客戶端的接口 
 * @see 這是第二種方式創(chuàng)建的客戶端,要用到的接口 
 */ 
package com.jadyer.server; 
import java.util.List; 
import com.jadyer.model.Person; 
import com.jadyer.model.User; 
public interface HelloService { 
 public String sayHello(String name); 
 public Person getPerson(User u); 
 public List<Person> getPersonList(Integer age, String name); 
} 
/** 
 * 第二種方式創(chuàng)建的客戶端,要用到的兩個POJO類 
 */ 
package com.jadyer.model; 
public class User { 
 private String name; 
 /*--getter和setter略--*/ 
} 
package com.jadyer.model; 
public class Person { 
 private Integer age; 
 private String name; 
 /*--getter和setter略--*/ 
} 

第三種方式:使用Ant通過WSDL文件來生成客戶端

package com.jadyer.client; 
/** 
 * 使用Ant通過WSDL生成客戶端 
 * @see 這里的ClientFromAnt.java是我自己創(chuàng)建的,并非Ant生成 
 * @see 這里要用到的JAR有:xfire-all-1.2.6.jar以及//xfire-distribution-1.2.6//lib//目錄中的所有JAR包 
 * @see 我們需要把這些JAR包都拷貝到Web Project//WebRoot//WEB-INF//lib//目錄中 
 * @see 然后把build.xml和MyFirstXFireServer.wsdl都拷貝到下Web Project的根目錄下即可 
 * @see 關于MyFirstXFireServer.wsdl文件,是我在WebServices服務啟動后 
 * @see 訪問http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl然后將其另存得到的 
 */ 
public class ClientFromAnt { 
 public static void main(String[] args) { 
 XFireServerClient client = new XFireServerClient(); 
 //String url = "http://127.0.0.1:8080/XFire_demo/services/XFireServer"; 
 //String result = client.getXFireServerHttpPort(url).sayHello("Jadyer33"); 
 //上面的兩行代碼,與下面的這一行代碼,同效~~ 
 String result = client.getXFireServerHttpPort().sayHello("Jadyer33"); 
 System.out.println(result); 
 } 
} 

用到的Ant文件,如下

<?xml version="1.0" encoding="UTF-8"?> 
<project name="wsgen" default="wsgen" basedir="."> 
 <path id="classpathId"> 
 <fileset dir="./WebRoot/WEB-INF/lib"> 
  <include name="*.jar" /> 
 </fileset> 
 </path> 
 <taskdef classpathref="classpathId" name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"/> 
 <target name="wsgen" description="generate client"> 
 <wsgen outputDirectory="./src/" wsdl="MyFirstXFireServer.wsdl" binding="xmlbeans" package="com.jadyer.client" overwrite="true"/> 
 </target> 
</project> 

也可以使用下面的這個Ant文件

<?xml version="1.0" encoding="UTF-8"?> 
<project name="xfireAnt" basedir="." default="createClientCode"> 
 <property name="xfirelib" value="${basedir}/WebRoot/WEB-INF/lib"/> 
 <property name="sources" value="${basedir}/src"/> 
 <path id="classpath"> 
 <fileset dir="${xfirelib}"> 
  <include name="*.jar"/> 
 </fileset> 
 </path> 
 <target name="createClientCode"> 
 <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="classpath"/> 
 <wsgen outputDirectory="${sources}" wsdl="http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl" package="com.jadyer.client" overwrite="true"/> 
 </target> 
</project> 

最后我再把MyFirstXFireServer.wsdl的內容,附加上

<?xml version="1.0" encoding="UTF-8"?> 
<wsdl:definitions targetNamespace="http://www.jadyer.com/XFireDemo" 
xmlns:tns="http://www.jadyer.com/XFireDemo" 
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" 
xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 
 <wsdl:types> 
 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   attributeFormDefault="qualified" 
   elementFormDefault="qualified" 
targetNamespace="http://www.jadyer.com/XFireDemo"> 
  <xsd:element name="sayHello"> 
  <xsd:complexType> 
   <xsd:sequence> 
   <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" /> 
   </xsd:sequence> 
  </xsd:complexType> 
  </xsd:element> 
  <xsd:element name="sayHelloResponse"> 
  <xsd:complexType> 
   <xsd:sequence> 
   <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" /> 
   </xsd:sequence> 
  </xsd:complexType> 
  </xsd:element> 
 </xsd:schema> 
 </wsdl:types> 
 <wsdl:message name="sayHelloRequest"> 
 <wsdl:part name="parameters" element="tns:sayHello"></wsdl:part> 
 </wsdl:message> 
 <wsdl:message name="sayHelloResponse"> 
 <wsdl:part name="parameters" element="tns:sayHelloResponse"></wsdl:part> 
 </wsdl:message> 
 <wsdl:portType name="XFireServerPortType"> 
 <wsdl:operation name="sayHello"> 
  <wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest"> 
  </wsdl:input> 
  <wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse"> 
  </wsdl:output> 
 </wsdl:operation> 
 </wsdl:portType> 
 <wsdl:binding name="XFireServerHttpBinding" type="tns:XFireServerPortType"> 
 <wsdlsoap:binding style="document" mce_style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
 <wsdl:operation name="sayHello"> 
  <wsdlsoap:operation soapAction="" /> 
  <wsdl:input name="sayHelloRequest"> 
  <wsdlsoap:body use="literal" /> 
  </wsdl:input> 
  <wsdl:output name="sayHelloResponse"> 
  <wsdlsoap:body use="literal" /> 
  </wsdl:output> 
 </wsdl:operation> 
 </wsdl:binding> 
 <wsdl:service name="XFireServer"> 
 <wsdl:port name="XFireServerHttpPort" binding="tns:XFireServerHttpBinding"> 
  <wsdlsoap:address location="http://127.0.0.1:8080/XFire_demo/services/XFireServer" /> 
 </wsdl:port> 
 </wsdl:service> 
</wsdl:definitions> 

第四種方法

這種方法用到了spring的jar包,是前幾天在找XFire+Spring的資料的時候看到的,在這里也是做個記錄。同樣的,這種方法和上面所提到的第二種方法在客戶端都需要與服務器一樣的接口,包名也必須一樣。

(1)在src目錄下新建client.xml(名字并非特定)

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
 <bean id="baseService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean" lazy-init="false" abstract="true"/>
<!-- id的名字作為標識,用于客戶端程序中獲取service,若有多個service咋在下面添加多個bean即可-->
 <bean id="MathService" parent="baseService">
 <property name="serviceClass">
 <value>service.MathService</value>
 </property>
 <property name="wsdlDocumentUrl">
<value>http://localhost:8080/myservice/mathWebService?wsdl</value>
 </property>
 </bean>
</beans> 

(2)在程序中調用服務代碼非常簡單

ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
MathService mathService = (MathService)ctx.getBean("MathService");
int result = mathService.add(int one,int two);

第五種辦法

先獲取到wsdl文件,命名為mathWebService.wsdl放在客戶端的src目錄下,接著通過程序訪問該wsdl文件,并調用需要的方法。

 String wsdl = "mathWebService.wsdl " ; // 對應的WSDL文件 
 Resource resource = new ClassPathResource(wsdl); 
 Client client = new Client(resource.getInputStream(), null ); // 根據(jù)WSDL創(chuàng)建客戶實例 
 Object[] objArray = new Object[ 2 ];
 objArray[ 0 ] = 2 ;
 obiArray[1] = 3;
  // 調用特定的Web Service方法 
 Object[] results = client.invoke( " add " , objArray);
 System.out.println( " result: " + results[ 0 ]);

對于這幾種方法,第一種方法如果傳遞的參數(shù)為服務器端的實體對象,這點好像比較麻煩,不知道在客戶端建立和服務器端相同的實體類行不行,沒有實踐,返回結果如果是復雜數(shù)據(jù)類型的話不知道有沒有什么問題,或者如何轉換,沒有深入研究。而且我個人覺得方法調用不是那么直觀。

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關文章

  • 利用spring?boot+WebSocket實現(xiàn)后臺主動消息推送功能

    利用spring?boot+WebSocket實現(xiàn)后臺主動消息推送功能

    目前對于服務端向客戶端推送數(shù)據(jù),常用技術方案有輪詢、websocket等,下面這篇文章主要給大家介紹了關于利用spring?boot+WebSocket實現(xiàn)后臺主動消息推送功能的相關資料,需要的朋友可以參考下
    2022-04-04
  • java實現(xiàn)遠程桌面的實例代碼

    java實現(xiàn)遠程桌面的實例代碼

    下面小編就為大家分享一篇java實現(xiàn)遠程桌面的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • idea工具欄如何添加快捷圖標的操作

    idea工具欄如何添加快捷圖標的操作

    這篇文章主要介紹了idea工具欄如何添加快捷圖標的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • SpringCloud的Eureka模塊詳解

    SpringCloud的Eureka模塊詳解

    這篇文章主要介紹了SpringCloud的Eureka模塊詳解,Spring Cloud 封裝了 Netflix 公司開發(fā)的 Eureka 模塊來實現(xiàn)服務治理,就是提供了微服務架構中各微服務實例的快速上線或下線且保持各服務能正常通信的能力的方案總稱,需要的朋友可以參考下
    2023-07-07
  • Java初級必看的數(shù)據(jù)類型與常量變量知識點

    Java初級必看的數(shù)據(jù)類型與常量變量知識點

    這篇文章主要給大家介紹了關于Java初級必看的數(shù)據(jù)類型與常量變量知識點的相關資料,需要的朋友可以參考下
    2023-11-11
  • SpringBoot整合Tomcat連接池的使用

    SpringBoot整合Tomcat連接池的使用

    這篇文章主要介紹了SpringBoot整合Tomcat連接池的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • springboot整合jquery和bootstrap框架過程圖解

    springboot整合jquery和bootstrap框架過程圖解

    這篇文章主要介紹了springboot整合jquery和bootstrap框架過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Java開發(fā)者推薦的10種常用工具

    Java開發(fā)者推薦的10種常用工具

    這篇文章主要為大家詳細介紹了Java開發(fā)者推薦的10種常用工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • SpringBoot整合jnotify實現(xiàn)針對指定目錄及其(動態(tài))子目錄的監(jiān)聽的方法

    SpringBoot整合jnotify實現(xiàn)針對指定目錄及其(動態(tài))子目錄的監(jiān)聽的方法

    本文介紹了JNotify這一Java庫在SpringBoot中的應用,JNotify允許應用程序監(jiān)聽文件系統(tǒng)事件,包括文件夾/文件的創(chuàng)建、刪除、修改和重命名,由于JNotify底層調用的關鍵部分是C語言開發(fā)的,所以在使用前需要在系統(tǒng)中加入相應的動態(tài)庫
    2024-10-10
  • SpringBoot使用布隆過濾器解決緩存穿透問題

    SpringBoot使用布隆過濾器解決緩存穿透問題

    緩存穿透是指當緩存系統(tǒng)中無法命中需要的數(shù)據(jù)時,會直接請求底層存儲系統(tǒng)(如數(shù)據(jù)庫),但是如果請求的數(shù)據(jù)根本不存在,那么大量的請求就會直接穿透緩存層,本文將給大家介紹一下SpringBoot使用布隆過濾器解決緩存穿透問題,需要的朋友可以參考下
    2023-10-10

最新評論