【Knowledge】Apex callout 与外部service的统合

本文详细介绍了如何通过Apex在Salesforce中实现与外部服务的集成,包括HTTP Callout和Web Service的区别,以及如何进行GET和POST测试。还探讨了Apex REST服务的创建和测试,包括认证机制和使用工具如StaticResourceCalloutMock和HttpCalloutMock进行测试的方法。
摘要由CSDN通过智能技术生成

概要

web service 和 http callout 的区别

通过Apex callout实现与外部service的接续,主要有2种类型

  • 以 WSDL 为 base 的 callout,使用 xml 形式接续外部soap web service
  • 使用 RESTJSON 形式的 http callout

区别

  • WSDL 的 callout 主要适用于 SOAP Web service。
  • Http 的 callout 使用的是 http service,既可以是 SOAP 也可以是 REST。

应用场景

  • 目前主流使用 REST,工作在应用层,代码少,JSON格式易读。
  • SOAP工作在网络层,主要是企业使用,主要为了统合原有application。

http callout 实装

外部 endpoint 的承认

连接外部service时,需要在sf系统首先承认外部endpoint

  • 設定 ⇒ クイック検索 ⇒ リモートサイトの設定
  • リモートサイトのURL 設定
    • 例① https://th-apex-http-callout.herokuapp.com
    • 例② https://th-apex-soap-service.herokuapp.com

callout的示意图

在这里插入图片描述

get 数据测试

在开发者console的匿名窗口中,输入下列代码测试

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
   
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
    for (Object animal: animals) {
   
        System.debug(animal);
    }
}

post 数据测试

开发者console的匿名窗口中,输入下列代码测试

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody('{"name":"mighty moose"}');
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 201) {
   
    System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
} else {
   
    System.debug(response.getBody());
}

合并上面两者到一个类中

public class AnimalsCallouts {
   
    public static HttpResponse makeGetCallout() {
   
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
   
            // Deserializes the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'animals' key as a list
            List<Object> animals = (List<Object>) results.get('animals');
            System.debug
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值