Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Here is my current controller:

@RestResource(urlMapping='/monitor')
global with sharing class TheController {

    @HttpGet
    global static String doGet() {

        Map<String, Object> status = new Map<String, Object>();
        status.put('key', 'value');
        return JSON.serialize(status);
    }
}

But this controller double encodes my json.

I found that sObjects are automatically serialized in rest endpoints:

When returning sObjects they are automatically serialized to JSON:

With that in mind I tried a controller like this one:

@RestResource(urlMapping='/monitor')
global with sharing class TheController {

    @HttpGet
    global static Map<String, Object> doGet() {

        Map<String, Object> status = new Map<String, Object>();
        status.put('key', 'value');
        return status;
    }
}

but then received the following error:

Save error: Invalid type for Http* method: Map.Object

What is the correct way to format a response for my REST endpoint?

share|improve this question

2 Answers 2

The serialization seems to require type information up front. So Map<String, String> would compile but is probably not what you want.

You can create simple inner classes with just properties and return graphs of those. I have implemented some REST APIs that way.

You can also do the serialization yourself by following this pattern:

@HttpGet
global static void get() {
    RestResponse res = RestContext.response;
    if (res == null) {
        res = new RestResponse();
        RestContext.response = res;
    }
    res.statusCode = 200;
    res.responseBody = Blob.valueOf(JSON.serialize(myObjectGraph()));
}
share|improve this answer

as an alternative to Keith C; I do the following (letting SFDC do the serializing for me)

 //  -------------------------------------------------------
 //  INNER CLASS ResponseWrapper : SFDC Apex Rest feature automatically serializes an instance of this class into JSON response to client
 //  -------------------------------------------------------
 global class ResponseWrapper {
    // my object graph goes in here
 }

  @HttpGet
  global static ResponseWrapper doGet () {      // corresponds to R in CRUD
  ResponseWrapper    respW    = new ResponseWrapper();
  RestRequest        restRqst    = System.RestContext.request;  
  RestResponse      restResp    = System.RestContext.response;

  // do my GET operations, building up member variables within ResponseWrapper
  restResp.statusCode = 200; // if you have errors, set this to something else like 400
  return respW;  // sfdc rest will serialize this for you
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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