2

Is it possible to make an apex httpMethod return two different kind of data type (DTOs).

When I try to do : global static Object myMethod()

I get this error : HttpGet methods do not support return type of Object.

8

Another common solution is to have a top-level DTO that contains the other two DTOs:

public class Response {
  public ResponseDTO response;
  public ErrorDTO[] errors;
}

This is particularly useful if you want to use the automatic XML/JSON conversion support provided by the platform depending on the Accept request header.

You can read more about this automatic serialization behavior here and its related sub-pages.

Edit:

You can actually use a virtual class, then have each DTO extend that class, giving you the ability to return either:

global virtual class Response {
    // stuff here //
}
global class DTO1 extends Response {
    // more stuff here //
}
global class DTO2 extends Response {
    // more stuff here //
}
@HttpGet global static Response doGet() {
    Response res;
    if(shouldDTO1()) {
      res = new DTO1();
    }
    if(shouldDTO2()) {
      res = new DTO2();
    }
    return res;
}
| improve this answer | |
  • 1
    Not tried it, but would having a base class and two specializations work here? E.g. declare the HTTP method as returning MyBaseClass but then return either MyExtendedClass1 or MyExtendedClass2 depending on which variant you want to send back? – Phil W Feb 26 at 16:24
  • 2
    @PhilW So, I just tested this and... it works! I'm pleasantly surprised. Adding an edit now. – sfdcfox Feb 26 at 16:37
  • Nice to see a bit of "real" OO happening in Apex :) – Phil W Feb 26 at 16:59
9

The response you return has to be of type Blob, so it doesn't matter what you return in the function. The way to return an object is by serializing your object as a JSON file, convert it to Blob object, and adding it to the responseBody of the RestResponse.

Here's an example:

@RestResource(urlMapping='/ServiceName/*')
global class SomeService {
    @HttpGet
    global static void getData(){
        RestResponse res = RestContext.response;
        try {
            List<Lead> leadsList = [SELECT Id 
                                     FROM Lead];
            res.statusCode = 200;
            res.responseBody = Blob.valueOf(JSON.serialize(leadsList));
        } catch (Exception e) {
            res.statusCode = 500;
            res.responseBody = Blob.valueOf(e.getMessage());
        }
    }
}
| improve this answer | |
  • I got a RestResource httpget method that takes form global static string, not void. – eaeaoo Feb 26 at 17:55
  • You can return whatever you want, but if you want to return it in the responseBody, it has to be Blob. – Dana Griff Feb 26 at 19:22
4

Yes, your method should return a String and you'll need to serialize your object in JSON before returning them.

global static String myMethod() {
 ...
 return JSON.serialize(myRecord);
}
| improve this answer | |
  • Dana's answer works as well if you want to work with status codes or other response properties. – POZ Feb 26 at 15:54

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.