I am confused about some of the information in the developer Guide regarding setting up a REST Service POST method. I've set up my service similarly to the following example from the Developer Guide:
@RestResource(urlMapping='/user_defined_type_example/*')
global with sharing class MyOwnTypeRestResource {
@HttpPost
global static MyUserDefinedClass echoMyType(MyUserDefinedClass ic) {
return ic;
}
global class MyUserDefinedClass {
global String string1;
global String string2 { get; set; }
private String privateString;
global transient String transientString;
global static String staticString;
}
}
This is the basics of my POST service. The only difference is that I use a list of user-defined class objects. Its likely these will only come one at a time, but in the interests of bulkification I figure you go with a list.
@RestResource(urlMapping='/RESTServices/*')
global class newService{
global class UserData{ // the user-defined class
public string username;
public string email;
public string phone;
}
@HttpPost
global static String postUserData(UserData[] userData){
//...do stuff
return JSONString;
}
}
OK, the Guide says: "Apex REST deserializes request data into public, private, or global class member variables of the user-defined type, unless the variable is declared as static or transient." - None of my variables are static or transient.
The service is expecting a list of UserData objects and I'm under the impression that APEX will receive the JSON string from the request body and deserialize it into a list of UserData objects.
1) Does this look right?? All the examples for Post methods that I see are sending the primitives as parameters into the POST method, as opposed to one of these user-defined classes that the Guide's example uses. I could do that, but the number of parameters is substantially larger than what I have provided in my example. So, is my assumption that APEX will received the JSON body in the request, and deserialize it into a list of userdata objects correct?? Or, do I really need to write out all those parameters? Or is something else supposed to happen that I'm missing completely?
2) In my test, I have created a JSON string that is validating and I want to get it into the test. I can't just add the string to the requestBody, as it asks me to make it a blob: req.requestBody - blob.valueOf(JSONRequestString); Is this the correct way to add the JSON String into the request?? That is, will the process see the requestbody as it comes in, convert the blob into a string, then deserialize the string into the UserData class object? My gut tells me 'NO!'. So, if not, how do I do this?
Any help from someone who has done this is much appreciated.