您的位置:首页 > 编程语言

调用WebService服务客户端代码编写

2012-11-08 22:51 429 查看
调用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;

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 调用

 方法同上,简单参数可以直接手写调用代码,复杂类型可以生成客户端代码。(为方便,当然手动也是可以的,如
4000
果你不嫌麻烦的话 -:))

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;

 

public class XFireClientSample {

 public static void main(String[] args) {

  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();

  }

 }

}

 

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);

 }

}

 

上述三种方式都是针对简单参数,简单方法等的调用,无需生成客户端代码等麻烦过程。如果是复杂的话,使用对应的技术的生成客户端代码也是很快的,可以尽快完成自己的工作任务。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐