0

我正在尝试调用这个 rest api 方法

 @HttpGet
    global static String retrievingNotificationSettings(){

     RestRequest req=RestContext.request;

    RestResponse res=RestContext.response;

      String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
     if(req.headers.get('Content-Type').equals('application/xml'))
     {
      Map<String, SObjectField> fields = Notification_Settings__c.SObjectType.getDescribe().fields.getMap();

      Schema.sObjectField T ;
      Notification_Settings__c ss;
      return 'hello World';

      }  


      if(req.headers.get('Content-Type').equals('application/json'))

    return System.JSON.serialize(note);


    return null;

我的要求是

GET /services/apexrest/v.9/notifications/preferences/ritesh HTTP/1.1
X-HostCommonName:
ap1.salesforce.com
Authorization:
OAuth 00D90000000j9AW!AQkAQLba73cDzjXhQ4kkQ2PSru4XpFuJcwr5kg_W_MkZmQnm9vI653FBWeJaABwClQqtJZD_b6j7V0O_elkzvkh7IqRKSUop
X-PrettyPrint:
1
Host:ap1.salesforce.com
X-Target-URI:
https://ap1.salesforce.com
Content-Type: application/json
Connection:Keep-Alive

我得到的回应是

HTTP/1.1 500 Internal Server Error
Date:
Fri, 25 Jan 2013 16:30:21 GMT
Content-Length:
203
Connection:
close
Content-Type:
application/json; charset=UTF-8
Server:

[
   {
    "message": "System.NullPointerException: Attempt to de-reference a null object\n\nClass.NotificationRestService.retrievingNotificationSettings: line 34, column 1",
    "errorCode": "APEX_ERROR"
  }
]

我得到错误的那一行是

if(req.headers.get('Content-Type').equals('application/xml'))

我不明白为什么在标题中我将值 Content-Type 作为 application/json 传递时出现此错误?请告诉我我在哪里犯错

4

1 回答 1

0

System.debug(req.headers)能看看它是否正确通过吗?

从字面上回答你的空指针问题 - 使用==而不是equals

if(req.headers.get('Content-Type') == 'application/xml')

但这并不能解决真正的根本问题。

您可能想尝试使用不同的标头(愚蠢,我知道)或决定添加 GET 参数,例如&mode=json

于 2013-01-25T22:38:13.037 回答