首页 技术 正文
技术 2022年11月15日
0 收藏 472 点赞 4,775 浏览 2985 个字

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
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898