利用axis调用webservice接口

来源:互联网 发布:美国海关数据库 编辑:程序博客网 时间:2024/05/19 13:14
package test;import javax.xml.namespace.QName;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import com.alibaba.fastjson.JSON;/***@author      created by Pjc*@date        2017年7月18日*@version     1.0*@problem*@answer*@action*/public class TestWebservice {public static void main(String[] args) {axis();}//通过axis方式调用webservice接口public static void axis() {try {// 指出service所在完整的URLString endpoint = "http://ip:端口号/项目名/webservice/sei(即webservice接口名)?wsdl";//调用接口的targetNamespaceString targetNamespace = "http://webservice接口所在的包名,逆序,一直到src下";//所调用接口的方法methodString method = "所要调用的方法名";// 创建一个服务(service)调用(call)Service service = new Service();Call call = (Call) service.createCall();// 通过service创建call对象// 设置service所在URLcall.setTargetEndpointAddress(new java.net.URL(endpoint));call.setOperationName(new QName(targetNamespace, method));call.setUseSOAPAction(true);//变量最好只是用String类型,其他类型会报错    call.addParameter(new QName(targetNamespace, "变量名"), org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);//设置参数名 state  第二个参数表示String类型,第三个参数表示入参  call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型// String path = targetNamespace + method;// call.setSOAPActionURI(path);String jsonString = (String) call.invoke(new Object[] {"变量值"});//此处为数组,有几个变量传几个变量//将json字符串转换为JSON对象JSON json = (JSON) JSON.parse(jsonString);//将接送对象转为java对象,此处用object代替,用的时候转换为你需要是用的对象就行了Object object = JSON.toJavaObject(json, Object.class);//注意别到错包com.alibaba.fastjson.JSONSystem.out.println(jsonString);} catch (Exception e) {e.printStackTrace();}}}