Salesforce调用外部的webservice接口

断了今生、忘了曾经 提交于 2020-10-27 00:45:37

一、上篇讲了如何将webservice接口生成wsdl并且部署到远程服务器上。外网可进行访问。

先将wsdl文件进行下载,然后再将wsdl导入到SF中。通过在Develop->Apex Classes中

然后点击下一步,可以得到下图

然后再去生成apex的代码。会生成两个类。AsyncWeb 一个是异步调用 web 一个是同步调用 要在远程站点设置访问的url

注意:此url要能够在外网也能访问,否则接口就调用不通

public class AsyncWeb {
    public class sendOAResponseFuture extends System.WebServiceCalloutFuture {
        public Boolean getValue() {
            web.sendOAResponse response = (web.sendOAResponse)System.WebServiceCallout.endInvoke(this);
            return response.return_x;
        }
    }
    public class AsyncSendServiceImplPort {
        public String endpoint_x = 'http://远程主机IP:8080/CXFWebservice/webservice/sendServie';
        public Map<String,String> inputHttpHeaders_x;
        public String clientCertName_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://siyuan.com/', 'web'};
        public AsyncWeb.sendOAResponseFuture beginSendOA(System.Continuation continuation,String param) {
            web.sendOA request_x = new web.sendOA();
            request_x.param = param;
            return (AsyncWeb.sendOAResponseFuture) System.WebServiceCallout.beginInvoke(
              this,
              request_x,
              AsyncWeb.sendOAResponseFuture.class,
              continuation,
              new String[]{endpoint_x,
              '',
              'http://siyuan.com/',
              'sendOA',
              'http://siyuan.com/',
              'sendOAResponse',
              'web.sendOAResponse'}
            );
        }
    }
}
public class web {
    public class sendOAResponse {
        public Boolean return_x;
        private String[] return_x_type_info = new String[]{'return','http://siyuan.com/',null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://siyuan.com/','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class sendOA {
        public String param;
        private String[] param_type_info = new String[]{'param','http://siyuan.com/',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://siyuan.com/','false','false'};
        private String[] field_order_type_info = new String[]{'param'};
    }
    public class SendServiceImplPort {
        public String endpoint_x = 'http://远程主机IP:8080/CXFWebservice/webservice/sendServie';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://siyuan.com/', 'web'};
        public Boolean sendOA(String param) {
            web.sendOA request_x = new web.sendOA();
            request_x.param = param;
            web.sendOAResponse response_x;
            Map<String, web.sendOAResponse> response_map_x = new Map<String, web.sendOAResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://siyuan.com/',
              'sendOA',
              'http://siyuan.com/',
              'sendOAResponse',
              'web.sendOAResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
    }
}

然后再在匿名类中,进行接口调用

web.SendServiceImplPort us = new web.SendServiceImplPort();
us.sendOA('hello ss');
System.debug('mess'+us.sendOA('hello ss'));

可以看到是调用成功的了

也输出了信息

 

现在去远程的服务器上看调用的一些打印信息

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!