salesforce 集成-REST API开发

来源:互联网 发布:淘宝退款不退货漏洞 编辑:程序博客网 时间:2024/06/01 10:25

场景:salesforce 提供了REST API接口,用于系统集成。但是在某些场景下需要定义一些业务流程,此时需要开发一个REST API来满足业务。

1. 接口开发

a) 注解

 i.  RestResource

1.  将改注解加到类的头部,将该类作为rest 资源暴露给外部系统

2.   类的限定为global.

3.  类似于java中servlet的注解

 ii. HttpGet

1.  将该注解加到静态方法的头部,将该方法作为rest资源暴露给外部系统。外部系统发送get请求的时候,调用该种类型的方法,并返回响应的资源。

2.  方法的限定为global static

3.   类似servlet中的doGet方法

  iii.HttpPost

1. 将该注解加到静态方法的头部,将该方法作为rest资源暴露给外部系统。外部系统发送post请求的时候,调用该种类型的方法,并返回响应的资源。

2. 方法的限定为global static

3.  类似servlet中doPost的方法

b)代码解析

i. get 请求

@RestResource(urlMapping='/TestDateRestService/*')global class TestDateRestService { @HttpGet global static void getData(){ RestRequest req = RestContext.request; RestResponse res = RestContext.response; String responseMsg = ''; //String start = req.params.get('start'); //start  String total = req.params.get('total');//total

外部系统发送get请求的方式为https://instance.salesforce.com/services/apexrest/TestDateRestService?start=10&total=20
getrData()方法通过RestContext方法获取request和response对象。而后get对应参数。RestRequest中包含很多方法,可以参考apexcode guid。另一种获取参数的方式如下,
这样系统默认就把start和total赋值到对应参数上。

@RestResource(urlMapping='/TestDateRestService/*')global class TestDateRestService {    @HttpGet    global static void getData(String start, String end){         RestRequest req = RestContext.request;        RestResponse res = RestContext.response;        String responseMsg = '';

当数据处理完成后,将数据已xml或者json的形式响应给外部系统

 //responseMsg = xmlHeader + responseMsg;        System.debug('[Custom Debug]responseMsg info is'+  responseMsg );        res.addHeader('Content-Type', 'application/xml');        res.responseBody = Blob.valueOf(responseMsg);

完整的代码:

@RestResource(urlMapping='/TestDateRestService/*')global class TestDateRestService {     @HttpGet    global static void getData(){         RestRequest req = RestContext.request;        RestResponse res = RestContext.response;        String responseMsg = '';        //start        String start = req.params.get('start');                //total        String total = req.params.get('total');         //validate params        RestServiceUtil.PaginationClass tempPagination = null;        tempPagination = RestServiceUtil.parsePagination(start,total);        if(tempPagination != null)        {                 if(tempPagination.result.success == false)            {                   responseMsg = tempPagination.result.message;                XmlStreamWriter w = new XmlStreamWriter();                w.writeStartDocument('UTF-8', '1.0');                w.writeStartElement(null, 'response', null);                w.writeStartElement(null, 'result', null);                w.writeStartElement(null, 'success', null);                w.writeCharacters(String.valueOf(tempPagination.result.success));//                w.writeEndElement(); //end success                w.writeStartElement(null, 'message', null);                w.writeCharacters(tempPagination.result.message);                w.writeEndElement(); //end message                w.writeEndElement(); //end result                w.writeEndElement(); //end response                                w.writeEndDocument();                responseMsg = w.getXmlString();            }            else            {                   Integer startInt = 0;                Integer totalInt = 0;                startInt = tempPagination.start;                totalInt = tempPagination.total;                long startCurrentTime1 = Datetime.now().getTime();                responseMsg = coreBussinessLogic(startInt,totalInt);                long endCurrentTime1 = Datetime.now().getTime();                system.debug('[Custom Debug]excute time is ' + (endCurrentTime1 - startCurrentTime1));                            }        }        else        {            responseMsg = '<response><result><success>false</success><message>请求内容异常,请发送正确请求!</message></result></response>';        }       //responseMsg = xmlHeader + responseMsg;        System.debug('[Custom Debug]responseMsg info is'+  responseMsg );        res.addHeader('Content-Type', 'application/xml');        res.responseBody = Blob.valueOf(responseMsg);    }}


post方法和测试程序后续介绍。。。

0 0
原创粉丝点击