java调用外部webservice的实例

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

 }
}

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

}

猜你喜欢

转载自blog.csdn.net/qq_35192741/article/details/83144782