首先調用接口時可使用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());
}
}
複製代碼