以下是基于Maven项目的客户端调用
第一步:Maven项目需要导入对应的axis依赖如下:
<!-- 引入axis调用远程webservice相关jar依赖 by dhj-->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.4</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
第二步:客户端java调用通用方法:
//通过axis方式调用webservice接口
public static void callWebservice() {
try {
// 指出service所在完整的URL
String endpoint = "http://ip:端口号/项目名/webservice/sei(即webservice接口名)?wsdl";
//调用接口的targetNamespace
String targetNamespace = "http://webservice接口所在的包名,逆序,一直到src下";(命名空间可以通过浏览器打开你的wsdl地址,里面对应的名称)
//所调用接口的方法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();
}
}
---------------------