调用WebService服务客户端代码编写
来源:互联网 发布:有什么好听的网络红歌 编辑:程序博客网 时间:2024/05/16 01:17
调用WebService服务客户端代码编写
目前比较流行的提供远程服务的技术中,WebService算是比较流行之一。因此,在调用WebService远程服务的客户端代码也是我们经常碰到的。本人把自己在开发中调用WebService远程服务的客户端代码总结下,算是留个笔记,方便以后使用。
1.使用Axis调用
如果提供的远程服务方法传入的参数都是简单类型,可以不用生成客户端代码,直接手动编写调用远程服务代码。
查看wsdl描述文件,wsdl:portType 暴露的是远程接口名称;wsdl:operation 对应的name 为远程接口暴露的方法,一般情况下 wsdl:definitions 定义的targetNamespace对应的是接口的包的反向。
import java.net.URL;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.client.Service;
public class AxisClientSample {
public static void main(String[] args) throws Exception {
String[] recipients = new String[]{}; //收件人
String strSubject = ""; //主题
String strContent = ""; //内容
String[] ccRecipients = new String[]{}; //抄送人
String[] bccRecipients = null; //密送人
String endPoint = "http://service_host:port/appProject/services/XXXXService";
Service server = new Service();
Call call = (Call) server.createCall();
call.setTargetEndpointAddress(new URL(endPoint));
//call.getMessageContext().setUsername("username"); //如果远程方法需要用户名和密码验证时
//call.getMessageContext().setPassword("password");
String resp = (String) call.invoke("exposeMethod", new Object[]{recipients,strSubject,strContent,ccRecipients,bccRecipients});
System.out.println(resp);
}
}
如果参数是复杂类型或者自定义pojo时,使用Axis对wsdl文件生成客户端代码,使用Axis的WSDL2JAVA生成客户端代码,编写调用代码如下:
XXXXServiceService service = new XXXXXLocator(); //获取远程服务
XXXXService stub = service.getXXXXService(); //XXXXService是远程服务接口类
//构建参数args , 实际调用远程服务方法
stub.exposeMethod(args);
2.XFire 调用
方法同上,简单参数可以直接手写调用代码,复杂类型可以生成客户端代码。(为方便,当然手动也是可以的,如果你不嫌麻烦的话 -:))
XFire 需要得到远程服务的接口类,可以根据wsdl描述得到对应的包名,接口名称和方法名,手动编写接口类,比如 XXXService.
import java.net.MalformedURLException;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class XFireClientSample {
public static void main(String[] args) {
Service srModel = new ObjectServiceFactory().create(XXXService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
Service srModel = new ObjectServiceFactory().create(XXXService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String endPoint = "http://service_host:port/app/services/XXXService";
try {
XXXService service = (XXXService) factory.create(srModel, endPoint);
String[] recipients = new String[] {}; // 收件人
String strSubject = ""; // 主题
String strContent = ""; // 内容
String[] ccRecipients = new String[] {}; // 抄送人
String[] bccRecipients = null; // 密送人
System.out.print("提交返回:" + service.getMethod(recipients, strSubject, strContent, ccRecipients, bccRecipients));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
try {
XXXService service = (XXXService) factory.create(srModel, endPoint);
String[] recipients = new String[] {}; // 收件人
String strSubject = ""; // 主题
String strContent = ""; // 内容
String[] ccRecipients = new String[] {}; // 抄送人
String[] bccRecipients = null; // 密送人
System.out.print("提交返回:" + service.getMethod(recipients, strSubject, strContent, ccRecipients, bccRecipients));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
3.使用CXF 调用
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import test.demo.service.XXXService;
public class CXFClientSample {
public static void main(String[] args) throws Exception{
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://remote_host:port/service/XXXService");
factory.setServiceClass(XXXService.class);
XXXService service = (XXXService) factory.create();
String username="sample";
String response = service.executeMethod(username);
System.out.println(response);
}
public static void main(String[] args) throws Exception{
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://remote_host:port/service/XXXService");
factory.setServiceClass(XXXService.class);
XXXService service = (XXXService) factory.create();
String username="sample";
String response = service.executeMethod(username);
System.out.println(response);
}
}
上述三种方式都是针对简单参数,简单方法等的调用,无需生成客户端代码等麻烦过程。如果是复杂的话,使用对应的技术的生成客户端代码也是很快的,可以尽快完成自己的工作任务。
- 调用WebService服务客户端代码编写
- 调用WebService服务客户端代码编写
- 使用axis编写客户端代码调用webservice的一些问题
- WebService调用客户端代码
- WebService客户端编写及调用
- 【web service 】 wsimport 生成客户端代码调用webservice 服务
- jdk annotation发布webservice服务 及生成客户端调用代码
- 作为客户端调用WebService服务java代码详解。
- Spring整合CXF,发布RSETful 风格WebService--编写客户端代码,调用RESTful WebService
- eclipse使用axis生成webservice客户端并且编写客户端调用代码
- 在客户端调用WebService服务获取数据
- 客户端调用Webservice服务(SSL)
- gsoap客户端调用几个webservice服务
- webService服务启动,客户端调用报错
- java客户端动态调用webservice服务
- java生成webservice客户端及服务调用
- WebService服务及客户端调用简单实例
- 使用axis2创建webservice步骤以及客户端调用service服务代码
- 单链表的创建及操作
- HDU 4460 Friend Chains 第37届ACM杭州赛区 H题
- json-lib总结
- Java使用总结
- RatingBar详解
- 调用WebService服务客户端代码编写
- c语言内存对齐的学习
- Java中的面向对象原理(续)(11.02)
- UVA10562 - Undraw the Trees
- 纯javascript版天气预报
- metinfo 5.0图片水印bug修改和支持透明背景png水印功能的增加
- 找出mysql 中谁持有表锁
- Struts2接收checkbox的值
- Oracle SCN headroom、ORA-19706 和_external_scn_rejection_threshold_hours 参数 说明