以下为调用代码
//类service需要导的包
import org.apache.axis.client.Service;
Service service = new Service();
Call call = service.createCall();
//需要访问的webservice地址http://ip:端口号/webservice发布地址?wsdl
call.setTargetEndpointAddress("http://ip:端口号/webservice发布地址?wsdl");
//QName参数 1,webservice的命名空间 2,需要访问的方法名
call.setOperationName(new QName("http://xxxxx/","xxx"));
//参数,对应要访问的方法参数,参数名字要和方法中参数一样
call.addParameter("aaa", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("bbb", XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter("ccc", XMLType.XSD_STRING,ParameterMode.IN);
System.out.println("开始接口调用");
//调用,返回的是xml结构的
Object o = call.invoke(new Object[]{"fdfds","eee","ggggg"});
System.out.println("获得返回值" + o);
webservice的命名空间,可以通过对方给的路径来获取
在浏览器输入地址http://ip:端口号/webservice发布地址?wsdl
红框中为webService的命名空间!
//另一种webservice请求方式,增加表头
发送webservice代码
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); String address = ip+webservice地址?wsdl"; org.apache.cxf.endpoint.Client client = dcf.createClient(address); log.info("拼接的ip地址是" + address); //增加表头的方法 client.getOutInterceptors().add(new ClientAuthInterceptor(“需要的用户名”, “需要的密码”)); //body json数据 Object[] obj = client.invoke("commonRequest", “json数据”); Object object = null; if (null != obj) { object = obj[0]; } if (null == object) { } else { //得到返回数据 String xmlString = object.toString(); log.info("返回数据成功!"); log.info(xmlString); }
//发送表头的类
package com.ptpec.ims.service.card; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * */ public class ClientAuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> { private String userName; private String passWord; public ClientAuthInterceptor(String userName, String passWord) { //准备发送阶段 super(Phase.PREPARE_SEND); this.userName = userName; this.passWord = passWord; } @Override public void handleMessage(SoapMessage message) throws Fault { List<Header> headers = message.getHeaders(); Document doc = DOMUtils.createDocument(); Element auth = doc.createElement("AuthenticationToken"); Element username = doc.createElement("Username"); username.setTextContent(userName); Element password = doc.createElement("Password"); password.setTextContent(passWord); auth.appendChild(username); auth.appendChild(password); headers.add(new Header(new QName(""), auth)); System.out.println("------------------"+headers); } }