WebService接口的俩种调用方式

第一种 使用 org.apache.axis.client.Call

public  String call(String operName, String endpoint, String[] paras,
            Object[] values) throws ServiceException, RemoteException {
            String result = null;
        
            // 直接引用远程的wsdl文件
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(endpoint);
            call.setOperationName(operName);// WSDL里面描述的接口名称
            call.setTimeout(15000);
            for (int i = 0; i < paras.length; i++) {
                call.addParameter(paras[i],
                        org.apache.axis.encoding.XMLType.XSD_STRING,
                        javax.xml.rpc.ParameterMode.IN);// 接口的参数
            }
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
            result = (String) call.invoke(values);
            // 给方法传递参数,并且调用方法
        return result;
    }

但在一次和对端接口的调试中 使用上述方法调不同接口  使用第二种得以解决

第二种 使用 org.apache.cxf.jaxws.JaxWsProxyFactoryBean

需要先定义一个接口

@WebService(name = "JkYfgBuildInfoSrv",targetNamespace="http://www.hxdi.com/spring-ws")
public interface JkYfgBuildInfoSrv {
    public String EMS_ImportJkYfgBuildInfoSrvRequest(@WebParam(name = "in") String in);
}

targetNamespace 是对端的wsdl的命名空间

方法名称是对端提供的 wsdl方法名   这里对端提供的 参数名只有in 

方法实现为

public void callBuildInfoSrv(String xml,String erNo) {
        JdbcTemplate jdbc = DataSourceUtils.getJdbcTemplate();
        comm = new CommonDao();
        comm.setJdbc(jdbc);
        String wsdlUrl = comm.getWsdl(erNo);
        
        String address = wsdlUrl.replace(".wsdl", "");
        
        String res = "";
        String logid = DBUtils.log4Before(jdbc, "IRMS", xml, erNo);
        boolean status = true;
        try {
            JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
            svr.setServiceClass(JkYfgBuildInfoSrv.class);
            svr.setAddress(address);
            JkYfgBuildInfoSrv ai = (JkYfgBuildInfoSrv) svr.create();
            Client proxy = ClientProxy.getClient(ai);
            
            //设置调用超时时间
            HTTPConduit conduit = (HTTPConduit) proxy.getConduit();  
            HTTPClientPolicy policy = new HTTPClientPolicy();  
            policy.setConnectionTimeout(5000);  
            policy.setReceiveTimeout(5000); 
            conduit.setClient(policy);
            
            Log.writeLog("调用接口入参request:" + xml);
            System.out.println("调用接口入参request:" + xml);
            res = ai.EMS_ImportJkYfgBuildInfoSrvRequest(xml);
            System.out.println("调用接口返回结果res:" + res);
            Log.writeLog("调用接口返回结果res:" + res);

            
        } catch (Exception e) {
            res = "调用接口失败";
            System.out.println("调用******接口失败");
            
            Log.writeLog("调用接口失败");
            
            e.printStackTrace();
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw, true));
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_35989077/article/details/85062244
今日推荐