I follow Creating REST APIs
And I would like to response data as:
{
"list": [
{
"id": "KH001"
},
...
]
}
My Class look like this
( everything just work as normal if I used items
instead of list
as variable name )
I am wondering would be possible if the response above is really required ?
@RestResource(urlMapping='/kh/*')
global with sharing class RESTKhController {
@HttpGet
global static ResponseWrapperClass getList() {
return new ResponseWrapperClass();
}
global class ResponseWrapperClass {
global List<DataWrapperClass> list; // ERROR unexpected token: 'list'
}
global class DataWrapperClass {
global String id;
}
}
Best Answer
Salesforce Apex and other languages like Java, C etc will give you errors when you try to use reserved words.
However as you said that this response is really required so here is what I suggest.
Rename the list to something unique example: list_replace_me
Instead of returning the wrapper from your GET method, return a string.
Manually serialize the object by
JsonGenerator
class to get a string, or useJSON.serialize(inputobject)
which return a string.Replace "list_replace_me" with "list" using
String.replace
function.Json Generator