Java调用webservice接口遇到的坑

首先调用接口时可以使用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());

    }
}

复制代码

转载于:https://juejin.im/post/5d00c5f8e51d4510617210aa

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值