[SalesForce] Apex rest service error: The requested resource does not exist

I am trying to build apex rest service which returns json/xml response . The api needs to support the below call

Sample Call:-

[API endpoint]/services.[format]

[API endpoint]/services/[service_code].[format]

Where format is json/xml

Here is my Issue when i call the service [API endpoint]/services it returns the correct result,but when i call the service

[API endpoint]/services.xml or [API endpoint]/services.json i get the below error

[{"errorCode":"NOT_FOUND","message":"The requested resource does not exist"}]

Can any one provide any pointers regarding the issue ?

Below is the apex rest service code:

@RestResource(urlMapping='/v2/services/*')
global with sharing class ServicesRestAPI_v2{

/*
    GET Service List / Get Service Definations

*/ 
@HttpGet
global static void getServices() {
     RestRequest req = RestContext.request; 
     RestResponse response = RestContext.response; 

     String format = req.requestURI.split('\\.').size()>1?req.requestURI.split('\\.')[1]:'';

     String responseStr;

     /*
      check to see whether jurisdiction_id is passed
     */
     String jId = req.params.get('jid');



     try{ 
         // see if a service code was  part of the URI
         String serviceCode= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
         serviceCode = serviceCode.split('\\.').size()>1?serviceCode.split('\\.')[0]:serviceCode;          

         if (serviceCode!= '' && !serviceCode.startsWith('services')) {
              //we have the service code here  : this is always done only if metadat =true
              if(req.headers.get('Content-Type')==APIConstants.CONTENT_TYPE_JSON || format=='json')  
                responseStr= ServicesAPIResponse.sendJSONResponse(ServicesHelper.getService(serviceCode),ServicesHelper.getServiceMetadata(serviceCode)); 
              else
                responseStr= ServicesAPIResponse.sendXMLResponse(ServicesHelper.getService(serviceCode),ServicesHelper.getServiceMetadata(serviceCode)); 
         } else {                
              if(req.headers.get('Content-Type')==APIConstants.CONTENT_TYPE_JSON || format=='json') 
                responseStr= ServicesAPIResponse.sendJSONResponse(ServicesHelper.listAllServices(jId)); 
              else
                responseStr= ServicesAPIResponse.sendXMLResponse(ServicesHelper.listAllServices(jId));                 
         } 

     }catch(CustomException e){             
            if(APIConstants.NOT_FOUND==e.getMessage()){
                response.statuscode =404;   
                responseStr= CustomException.sendJSONApiErrorResponse('Invalid service_code provided');            
            }               
     } 

     RestContext.response.responseBody =   Blob.valueOf(responseStr);  

}

Best Answer

I have a service that returns JSON for .json and XML for .xml or no extension.

I notice that your URL mapping is:

urlMapping='/v2/services/*'

which I assume will not match either of these (try them):

[API endpoint]/services.json
[API endpoint]/services.xml

but would match either of these:

[API endpoint]/services/[service_code].json
[API endpoint]/services/[service_code].xml

Perhaps you need to introduce a "none" or "unknown" dummy service code or use the rather ugly:

[API endpoint]/services/.json
[API endpoint]/services/.xml

PS

On the status code question, this is what I have done in a couple of places:

@HttpGet
global static void get() {
    RestResponse res = RestContext.response;
    if (res == null) {
        res = new RestResponse();
        RestContext.response = res;
    }
    try {
        res.statusCode = 200;
        res.responseBody = Blob.valueOf(readConfiguration());
    } catch (EndUserMessageException e) {
        res.statusCode = 400;
        res.responseBody = Blob.valueOf(e.getMessage());
    } catch (Exception e) {
        res.statusCode = 500;
        res.responseBody = Blob.valueOf('SERVER ERROR\n\n' + String.valueOf(e) + '\n\n' + e.getStackTraceString());
    }
}