我尝试使用下面的 .json 得到错误的请求错误,但我没有得到。我想发送这个响应。
例如:
验证错误:{ "StatusCode":"400", "id":"", "error":"xxxx" } 2.Internal Server error: { "StatusCode":"500", "id":"", "错误":"xxxx" }
Apex REST API 类:
@RestResource(urlMapping='/IoT_Case__c/*')
全局共享类 IOT_CaseManager { 全局类 ResponseWrapper{public String StatusCode; public String StatusMessage; public String ErrorMessage; public String ID; //public List<IoT_Case__c> Details; } @HttpPost global static ResponseWrapper createIOT_Case(String IOT_Case_Type, String Name,String Email,String Phone,String Facility_Name, String Address,String case_Reason,string Device_ID, String Date_time, String Pool_Name){ RestRequest request = RestContext.request; ResponseWrapper resp = new ResponseWrapper(); system.debug('%%%%%% the response is######'+resp); List<IoT_Case__c> caseList = new List< IoT_Case__c> (); try{ IoT_Case__c Iot_case = new IoT_Case__c (); Iot_case.IOT_Case_Type__c=IOT_Case_Type; Iot_case.Name__c= Name; Iot_case.Email__c= Email; Iot_case.Phone__c= Phone; Iot_case.Address__c= Address; Iot_case.Pool_Name__c= Pool_Name; Iot_case.Device_ID__c= Device_ID; Iot_case.case_Reason__c=case_Reason; Iot_case.Facility_Name__c=Facility_Name; Iot_case.Date_time__c=Date.valueOf(Date_time); caseList.add(Iot_case); if (caseList.size() > 0){ List<IoT_Case__c> Iotcase_List = [SELECT ID,Name__c,case_Reason__c,Device_ID__c,IOT_Case_status__c from IoT_Case__c]; // system.debug('@@@@the value is &&&&&'+ Iotcase_List); For (Iot_case__c ic:Iotcase_List){ if ((ic.IOT_Case_status__c == 'Open'|| ic.IOT_Case_status__c == 'In -Progress') && (ic.case_Reason__c == caseList[0].case_Reason__c && ic.Device_ID__c == caseList[0].Device_ID__c)){ resp.errorMessage='Duplicate case'; system.debug('@@@@the value is &&&&&'+resp.errorMessage ); } } if(resp.errorMessage!='Duplicate case'){ insert caseList; resp.statusCode ='201'; resp.statusMessage ='Success' ; } else{ resp.statusCode ='409'; resp.statusMessage ='Duplicate' ; } } } catch (Exception e) { system.debug('&&&&&&& The errro message is *****'+ e); if(e.getMessage().contains('Validation')) { resp.statusCode = '400'; resp.statusMessage = 'Exception : ' + e.getMessage(); } if(e.getMessage().contains('Internal server error ')) { resp.statusCode = '500'; resp.statusMessage = 'Exception : ' + e.getMessage(); } } string Outputget= JSON.serialize((caseList)); //resp.Details = caseList; resp.ID = caseList[0].Id; system.debug('@@@@@@the response id is&&&&&'+resp.ID ); //resp.ErrorMessage= 'Exception ' ; return resp; } } json : { "IOT_Case_Type": "Equipment_Replacement", "Name": "test123", "Email": "test1@gmail.com", "Phone": "9086978010", "Address": "Chennai", "Pool_Name": "Pool-1", "Facility_Name": "Facility-1", "Device_ID" : "D1110" , "case_Reason" : "FAC Sensor Expired,ORP Sensor Expired,pH Sensor Expired", "Date_time": "2021-02-01T03:42:22Z" } endpoint Post url :/services/apexrest/IoT_Case__c/*