在Salesforce中調用外部系統所提供的的Web Service


這里需要提供外部service所對應的WSDL文件(Salesforce只支持從本地上傳),並且提供的WSDL文件有如下兩點要求:

1):wsdl 文件只能有一個binding,Salesforce是不支持多個binding的

2):wsdl 文件要包含所有的schema信息,Salesforce是不支持external schema的import的

 

接下來便是具體的操作步驟:

1):按照此目錄 Setup --> Build --> Develop --> Apex Classes 找到對應的 Generate from WSDL 按鈕,如下圖所示

 

2):點擊上圖中的Generate from WSDL按鈕,會得到如下視圖,點擊Browser按鈕從本地目錄選擇相對應的WSDL文件

 

3):之后點擊下圖中的Parse WSDL按鈕,會將WSDL轉換成Apex的Class文件(如果此處出現URL無法訪問的異常,請看第6條的解決方案)

 

4):如果沒有任何異常出現的話,就可以在下圖中填入Apex Class Name了,然后點擊Generate Apex code去真正的生成對應的Class文件

 

5):在如下圖中可以查看我們具體生成的Apex Class文件,之后就可以在對應的Controller里去調用此Class中的方法了(實際上就是call service),具體代碼這里就省略了

 

6):如果在第3步的時候出現URL無法訪問的異常,根據異常信息可以到Remote Site Setting中把WSDL文件中用到URL添加到Salesforce中

 

7):當然了在Salesforce中同樣可以調用外部所提供的Rest Service,並且調用Rest Service的方式顯得更加的簡潔。

簡單的調用代碼如下所示:

public void callRestService() {

        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
    
        req.setEndpoint('http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/');
        req.setMethod('GET');
        //req.setHeader('Content-Type', 'application/json');
        //req.setHeader('Accept', 'application/json');
        //req.setBody('');
        req.setCompressed(true); // otherwise we hit a limit of 32000
    
        try {
            res = http.send(req);
            system.debug('-----000001----- successful response: ' + res);
            system.debug('-----000001----- successful response string: ' + res.toString());
             system.debug('-----000001----- successful response STATUS: '+res.getStatus());
             system.debug('-----000001----- successful response STATUS_CODE: '+res.getStatusCode());
             system.debug('-----000001----- successful response Content: ' + res.getBody());
        } catch(System.CalloutException e) {
            System.debug('-----000002----- Callout error: '+ e);
            System.debug('-----000003----- failed response:' + res.toString());
        }
    
    }

 

 更多細節請看此鏈接: https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM