java调用外部webservice的实例

本文介绍如何使用Java通过Apache Axis库调用Web Services,包括设置服务地址、方法名、参数及返回类型,同时展示了如何解析返回的XML数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.hj.services.webservices;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class WebServicesClient {
 public static void main(String[] args) throws Exception {
  //new 一个服务
  Service sv = new Service();
  //创建一个call对象
  Call call = (Call) sv.createCall();
  //设置要调用的接口地址以上一篇的为例子
  call.setTargetEndpointAddress(new URL("http://192.168.200.39:8080/creazy"));
  //设置要调用的接口方法
  call.setOperationName(new QName("getUsers"));
  //设置参数名 id  第二个参数表示String类型,第三个参数表示入参
  call.addParameter("id", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
  //返回参数类型
  call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
  //开始调用方法,假设我传入的参数id的内容是1001   调用之后会根据id返回users信息,
  // 以xml格式的字符串返回,也可以json格式主要看对方用什么方式返回
  String result = (String) call.invoke(new Object[]{"1001"});
  //打印字符串
  System.out.println(result);
  //转成Document对象
  Document doc = DocumentHelper.parseText(result);
  //用dom4j方式拿到xml的根节点然后打印结果信息
  Element root = doc.getRootElement();
  System.out.println("id="+root.element("UsersID").getText()+"    name="+root.element("UsersName").getText()+"     sex="+root.element("UsersSex").getText());

 }
}
/**
     * 将数据发送到对方 webservice  接口里
     * @param
     * @return
     * @throws Exception
     */
    public static String sendMethod(String  xml,String wsdl_ulr,String OperationCode) throws Exception
    {
        //new 一个服务
        Service sv = new Service();
        //创建一个call对象
        Call call = (Call) sv.createCall();
        //设置要调用的接口地址以
        call.setTargetEndpointAddress(new URL(wsdl_ulr));
        //设置要调用的接口方法
        //new QName的URL是要指向的命名空间的名称,这个URL地址在你的wsdl打开后可以看到的,
        //上面有写着targetNamespace="http://*.*.*/",这个就是你的命名空间值了;
        call.setOperationName(new QName("http://10.0.0.0:7002/webapp/services/DTIService","processMessage"));
        //设置参数名 id  第二个参数表示String类型,第三个参数表示入参
        call.addParameter("arg0", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
        call.addParameter("arg1", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
        call.addParameter("arg2", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
        call.addParameter("arg3", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
        //返回参数类型
        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
        //开始调用方法,以xml格式的字符串返回,也可以json格式主要看对方用什么方式返回
        String result = (String) call.invoke( new Object[] { "03", "00", OperationCode, xml });
        //打印字符串
        System.out.println(result);
        return result;
    }

XFire方式

   public boolean CallWebServiceCaseSender(String xmlString, String url)throws MalformedURLException {
        //ICaseWrite.class  为接口
        org.codehaus.xfire.service.Service serviceModel = new ObjectServiceFactory().create(ICaseWrite.class);
        ICaseWrite servcie;
        servcie = (ICaseWrite) new XFireProxyFactory().create(serviceModel,url);
        boolean bs = servcie.caseWrite(xmlString);

        return bs;
    }

public interface ICaseWrite {
	
	public boolean synchXfDclx(String caseid);
	

	public boolean caseWrite(String xml);
	

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值