Issue:-
Recently i got one requirement to create one REST API which should support JSON and XML both. If we will pass JSON request body then response should come in JSON. But if we will send request body in XML then response should come in XML.
Solution:-
So here is solution :)
Global class REST_API_Helper{
public static blob formatResponse(ResponseBase ResponseBaseObj,String contentType){
String response;
String strContectType = contentType.toUpperCase();
if(strContectType.contains('XML')){
response = serializeRespinseBaseXml(ResponseBaseObj);
}
else {
response = JSON.serialize(ResponseBaseObj);
}
return blob.valueOf(response);
}
/*
*************************************************************************
* @description: This function is used to send the response in XML
* @return: string
*************************************************************************
*/
public static string serializeRespinseBaseXml(ResponseBase ResponseBaseObj ){
String responseXML = '';
responseXML += '<response>';
XmlStreamWriter w = new XmlStreamWriter();
w.writeStartElement(null , 'Message', null );
w.writeCharacters(ResponseBaseObj.Message);
w.writeEndElement();
w.writeStartElement(null , 'Success', null );
w.writeCharacters(String.valueOf(ResponseBaseObj.Success));
w.writeEndElement();
responseXML += '</response>';
String xmlOutput = '<?xml version="1.0" encoding="UTF-8"?>' + '<Response>' + w.getXmlString()
+ '</Response>';
w.close();
return xmlOutput ;
}
global class ResponseBase {
global Boolean Success {get;set;}
global String Message {get;set;}
global ResponseBase(){
success = true;
message = 'Records are updated';
}
}
} |
@RestResource(urlMapping='/api/*/createContact')
global with sharing class RESTAPI_JSON_XML
{
@HttpPost
global static void doPost(ContactDetail Detail) {
RestResponse standardResp = RestContext.response;
REST_API_Helper.ResponseBase CustomeResponse = new REST_API_Helper.ResponseBase();
ContactDetail reqBodyObj = Detail;
RestRequest req = RestContext.request;
String ContentType = RestContext.request.headers.get('Content-Type') ;
Contact cont = new Contact();
cont.FirstName = reqBodyObj.FirstName.trim();
cont.LastName = reqBodyObj.LastName.trim();
insert cont;
CustomeResponse.Success = true;
CustomeResponse.Message = 'Contact Created id ='+cont.id;
standardResp.statusCode = 200;
standardResp.responseBody = REST_API_Helper.formatResponse(CustomeResponse,ContentType);
}
global class ContactDetail{
global string FirstName {get;set;}
global string LastName {get;set;}
public ContactDetail() {}
}
}
Sample JSON Example:-
Method:- Post
URL:- /services/apexrest/api/*/createContact
Request Body
{
"Detail": {
"FirstName":"Amit",
"LastName" :"Chaudhary"
}
}
Sample XML Example:-
Method:- Post
URL:- /services/apexrest/api/*/createContact
Request Headers:-
Content-Type: application/xml; charset=UTF-8
Accept: application/xml
Request Body
<?xml version="1.0" encoding="UTF-8"?>
<request>
<Detail>
<FirstName>566788</FirstName>
<LastName>V00001397450513</LastName>
</Detail>
</request>
Response :-
<?xml version="1.0" encoding="UTF-8"?><Response><Message>Contact Created id =0039000002SMSFJAA5</Message><Success>true</Success></Response>