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