could you please point out my error? I have a very simple class and a webservice:
public class JSONFactory {
public Integer id;
public String name;
public String lastName;
public JSONFactory(Integer a, String b, String c){
this.id = a;
this.name = b;
this.lastName = c;
}
}
@RestResource(urlMapping='/Johnny')
global class WebServiceFactory {
@HttpGet
global static String getRecord() {
List<JSONFactory> JSONs = new List<JSONFactory>();
for(integer i = 0; i<4; i++){
JSONs.add(new JSONFactory(i, 'Jan' + i, 'Test' + i));
}
return JSONs.toString();
}
}
The web service should return a JSON made of a list of objects. According to the documentation, this happens automatically:
The response is either the serialized form of the method return value or it's the value of the responseBody property based on the following rules:
- If the method returns void, then Apex REST returns the response in the responseBody property. If the method returns a value, then Apex
- REST serializes the return value as the response.
After calling the getRecord method, the system debugs:
14:45:08:352 USER_DEBUG [43]|DEBUG|"(JSONFactory:[id=0, lastName=Test0, name=Jan0], JSONFactory:[id=1, lastName=Test1, name=Jan1], JSONFactory:[id=2, lastName=Test2, name=Jan2], JSONFactory:[id=3, lastName=Test3, name=Jan3])"
This is not a valid JSON and I am having trouble deserializing it into a list of Objects:
List<Object> objs = (List<Object>)JSON.deserializeUntyped(res.getBody());
I'm getting an exception:
FATAL_ERROR System.TypeException: Invalid conversion from runtime type String to List<ANY>