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所在完整的URL
String endpoint = "http://ip:端口号/项目名/webservice/sei(即webservice接口名)?wsdl";
//调用接口的targetNamespace
String targetNamespace = "http://webservice接口所在的包名,逆序,一直到src下";
//所调用接口的方法method
String method = "所要调用的方法名";
// 创建一个服务(service)调用(call)
Service service = new Service();
Call call = (Call) service.createCall();// 通过service创建call对象
// 设置service所在URL
call.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.JSON
System.out.println(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
利用axis调用webservice接口
最新推荐文章于 2022-12-08 17:15:41 发布