搜尋
首頁Javajava教程XFire建構web service客戶端的五種方式

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

① 透過WSDL位址來建立動態客戶端 
② 透過服務端提供的介面來建立客戶端 
③ 使用Ant透過WSDL檔來產生客戶端

第一種方式:透過WSDL位址來建立動態客戶端

package com.jadyer.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
/**
 * 通过WSDL来创建动态客户端
 * @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]);
 }
}

   

第二種方式:透過服務端提供的連接埠來用客戶端

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服务端提供的接口来创建客户端
 * @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接口创建一个服务模型serviceModel
 //serviceModel包含服务的说明,换句话说,就是服务的元数据
 //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源码发现
 //下面两行代码与这里直接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 这是第二种方式创建的客户端,要用到的接口
 */
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);
}
/**
 * 第二种方式创建的客户端,要用到的两个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是我自己创建的,并非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>

   

天在找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 ); // 根据WSDL创建客户实例
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 ]);

   

對於這幾種方法,第一種方法如果傳遞的參數為伺服器端的實體對象,這點好像比較麻煩,不知道在客戶端建立和伺服器端相同的實體類行不行,沒有實踐,返回結果如果是複雜資料類型的話不知道有沒有什麼問題,或者如何轉換,沒有深入研究。而且我個人覺得方法呼叫不是那麼直覺。

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

更多XFire建構web service客戶端的五種方式相關文章請關注PHP中文網!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
是否有任何威脅或增強Java平台獨立性的新興技術?是否有任何威脅或增強Java平台獨立性的新興技術?Apr 24, 2025 am 12:11 AM

新興技術對Java的平台獨立性既有威脅也有增強。 1)雲計算和容器化技術如Docker增強了Java的平台獨立性,但需要優化以適應不同雲環境。 2)WebAssembly通過GraalVM編譯Java代碼,擴展了其平台獨立性,但需與其他語言競爭性能。

JVM的實現是什麼,它們都提供了相同的平台獨立性?JVM的實現是什麼,它們都提供了相同的平台獨立性?Apr 24, 2025 am 12:10 AM

不同JVM實現都能提供平台獨立性,但表現略有不同。 1.OracleHotSpot和OpenJDKJVM在平台獨立性上表現相似,但OpenJDK可能需額外配置。 2.IBMJ9JVM在特定操作系統上表現優化。 3.GraalVM支持多語言,需額外配置。 4.AzulZingJVM需特定平台調整。

平台獨立性如何降低發展成本和時間?平台獨立性如何降低發展成本和時間?Apr 24, 2025 am 12:08 AM

平台獨立性通過在多種操作系統上運行同一套代碼,降低開發成本和縮短開發時間。具體表現為:1.減少開發時間,只需維護一套代碼;2.降低維護成本,統一測試流程;3.快速迭代和團隊協作,簡化部署過程。

Java的平台獨立性如何促進代碼重用?Java的平台獨立性如何促進代碼重用?Apr 24, 2025 am 12:05 AM

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

您如何在Java應用程序中對平台特定問題進行故障排除?您如何在Java應用程序中對平台特定問題進行故障排除?Apr 24, 2025 am 12:04 AM

要解決Java應用程序中的平台特定問題,可以採取以下步驟:1.使用Java的System類查看系統屬性以了解運行環境。 2.利用File類或java.nio.file包處理文件路徑。 3.根據操作系統條件加載本地庫。 4.使用VisualVM或JProfiler優化跨平台性能。 5.通過Docker容器化確保測試環境與生產環境一致。 6.利用GitHubActions在多個平台上進行自動化測試。這些方法有助於有效地解決Java應用程序中的平台特定問題。

JVM中的類加載程序子系統如何促進平台獨立性?JVM中的類加載程序子系統如何促進平台獨立性?Apr 23, 2025 am 12:14 AM

類加載器通過統一的類文件格式、動態加載、雙親委派模型和平台無關的字節碼,確保Java程序在不同平台上的一致性和兼容性,實現平台獨立性。

Java編譯器會產生特定於平台的代碼嗎?解釋。Java編譯器會產生特定於平台的代碼嗎?解釋。Apr 23, 2025 am 12:09 AM

Java編譯器生成的代碼是平台無關的,但最終執行的代碼是平台特定的。 1.Java源代碼編譯成平台無關的字節碼。 2.JVM將字節碼轉換為特定平台的機器碼,確保跨平台運行但性能可能不同。

JVM如何處理不同操作系統的多線程?JVM如何處理不同操作系統的多線程?Apr 23, 2025 am 12:07 AM

多線程在現代編程中重要,因為它能提高程序的響應性和資源利用率,並處理複雜的並發任務。 JVM通過線程映射、調度機制和同步鎖機制,在不同操作系統上確保多線程的一致性和高效性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境