XFire是codeHaus組織提供的一個WebService開源框架,目前已被Apache的CXF所取代,已很少有人用了,這里簡單記錄下其調用WebService使用方法。官網現已不提供下載,可以到maven倉庫下載,下載地址:https://search.maven.org/artifact/org.codehaus.xfire/xfire-distribution/1.2.6/jar。文中demo所使用到的軟件版本:Java 1.8.0_191、Xfire 1.2.6。
1、准備
參考Java調用WebService方法總結(1)--准備工作
2、調用
2.1、Client方式
public static void client(String param) { try { Client client = new Client(new URL("http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl")); Object[] results = client.invoke("toTraditionalChinese", new Object[]{param}); System.out.println(results[0]); } catch (Exception e) { e.printStackTrace(); } }
如果服務端是用JAX-WS發布的,需指定RPC方式(接口類上增加@SOAPBinding(style = SOAPBinding.Style.RPC));調用本地的服務:
/** * JAX-WS發布的服務端需指定RPC方式 * @param param */ public static void client2(String param) { try { Client client = new Client(new URL("http://10.40.103.48:9006/zsywservice/TestService?wsdl")); Object[] results = client.invoke("hello", new Object[]{param}); System.out.println(results[0]); } catch (Exception e) { e.printStackTrace(); } }
2.2、ServiceFactory方式
這種方式需要把接口類拷貝到客戶端。測試時調用本地服務時參數傳不進去,總是null;不知為何;有了解的朋友請指正。
/** * 調用本地服務時參數傳不進去,總是null * @param param */ public static void factory(String param) { try { Service serviceModel = new ObjectServiceFactory().create(ITestService.class, null, "http://ws.zsyw.inspur.com/", null); XFireProxyFactory factory = new XFireProxyFactory(); ITestService testService = (ITestService) factory.create(serviceModel, "http://10.40.103.48:9006/zsywservice/TestService?wsdl"); String result = testService.hello(param); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } }
2.3、完整代碼
package com.inspur.ws; import java.net.URL; import org.codehaus.xfire.client.Client; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.inspur.zsyw.ws.ITestService; /** * * xfire調用WebService * */ public class Xfire { public static void client(String param) { try { Client client = new Client(new URL("http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl")); Object[] results = client.invoke("toTraditionalChinese", new Object[]{param}); System.out.println(results[0]); } catch (Exception e) { e.printStackTrace(); } } /** * JAX-WS發布的服務端需指定rpc方式 * @param param */ public static void client2(String param) { try { Client client = new Client(new URL("http://10.40.103.48:9006/zsywservice/TestService?wsdl")); Object[] results = client.invoke("hello", new Object[]{param}); System.out.println(results[0]); } catch (Exception e) { e.printStackTrace(); } } /** * 調用本地服務時參數傳不進去,總是null * @param param */ public static void factory(String param) { try { Service serviceModel = new ObjectServiceFactory().create(ITestService.class, null, "http://ws.zsyw.inspur.com/", null); XFireProxyFactory factory = new XFireProxyFactory(); ITestService testService = (ITestService) factory.create(serviceModel, "http://10.40.103.48:9006/zsywservice/TestService?wsdl"); String result = testService.hello(param); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { client("大學"); client2("大學"); factory("大學");//hello,null } }
