Java调用WebService方法总结(6)--XFire调用WebService

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
}
}
上一篇:TS学习随笔(二)->类型推论,联合类型


下一篇:TS学习随笔(七)->声明文件