首先调用接口时可使用axis方式调用
在调用接口时若抛异常:org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
此时须要排查返回类型是什么
call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING);
package com.irs.util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.List;
public class WebServiceUtil {
public static Object webServiceApi(String endpoint,String nameSpance,String methodName,String parameters){
try {
List<String> temp=new ArrayList<>();
// String endpoint = "http://***?wsdl";
//直接引用远程的wsdl文件
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName(new QName(nameSpance, methodName));
//对parameters对json进行分割
JSONObject job = null;
JSONArray arr = JSONArray.fromObject(parameters);
for (int i = 0; i < arr.size(); i++) {
job = arr.getJSONObject(i);
System.out.println(job);
job = arr.getJSONObject(i);
Object paramName = job.get("paramName");
Object paramType = job.get("paramType");
Object paramValue = job.get("paramValue");
temp.add(String.valueOf(paramValue));
call.addParameter( String.valueOf(paramName), org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);//接口的参数
}
//判断数组中的数据是xml仍是字符串
int i=0;
for (String s : temp) {
if (s.contains("<")){
i=1;
}
}
if (i==0){
call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_VECTOR);//设置返回类型
// call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING);//设置返回类型
// call.setReturnClass(String.class);
}else {
call.setReturnClass(String.class);
}
Object[] array2=temp.toArray();
Object result=call.invoke(array2);
return result;
}
catch (Exception e) {
System.err.println(e.toString());
}
return null;
}
public static void main(String[] args) {
//[{"paramName":"参数名","paramType":"string","paramValue":"****"},{"paramName":"参数名","paramType":"string","paramValue":"参数值"}]
Object result=webServiceApi("http://***?wsdl","http://namespace","getCbdwxx","[{\"paramName\":\"***\",\"paramType\":\"string\",\"paramValue\":\"***\"}]");
System.out.println(result.toString());
}
}
复制代码