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:

global with sharing class ServicesRestAPI_v2{

    GET Service List / Get Service Definations

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');

         // 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)); 
                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)); 
                responseStr= ServicesAPIResponse.sendXMLResponse(ServicesHelper.listAllServices(jId));                 

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

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


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

I notice that your URL mapping is:


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


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

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());
  • no that does not work either – Sneha Aug 25 '14 at 11:30
  • @Sneha Strange. Your are including your "v2" in the path aren't you? I suggest you find a case that works and start from there. – Keith C Aug 25 '14 at 13:27
  • yes i am adding v2 in the path. The problem started when i change the return type from String to void and added the response in this RestContext.response.responseBody = Blob.valueOf(responseStr); – Sneha Aug 25 '14 at 16:22
  • @Sneha In code where I set the responseBody I also set the statusCode to 200 when everything is OK and other values for errors. – Keith C Aug 25 '14 at 16:30
  • Sorry did not understand..You mean should i add status code? – Sneha Aug 25 '14 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.