I'm new to Java so this may not be related to AWS lambda at all. However, lambda takes such liberties with input/output objects that I'm assuming it's the culprit here.
I'm building my first lambda function and want to return a simple JSON structure (simplified further for this example):
{
"document" : "1",
"person" : { "name" : "John Doe" }
}
However, when lambda serializes the JSON it always sets "person" to a blank object!
{
"document": "1",
"person": {}
}
Here is my code in full:
- test1.java
package handler_test;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class test1 implements RequestHandler<String, ResponseClass> {
@Override
public ResponseClass handleRequest(String input, Context context) {
return new ResponseClass();
}
}
- ResponseClass.java
package handler_test;
import org.json.JSONException;
import org.json.JSONObject;
public class ResponseClass {
String document;
JSONObject person;
public String getdocument() {
return "1";
}
public JSONObject getperson() {
try {
return new JSONObject("{ \"name\" : \"John Doe\" }");
} catch (JSONException e1) {
System.out.println("error creating jsonobject");
return null;
}
}
public ResponseClass() {
}
}
I've tried this with dozens of variations, using objects instead of JSONObjects, converting getperson output to a string (which works, if I wanted a string), etc. If I store the values and print them to the logger, it's fine. But as soon as I try to return it through lambda it goes pear-shaped. I've combed the 'net and can't find anything more on AWS-lambda java response objects beyond Amazon's "greetings" sample code, which just includes two strings. Any suggestions greatly appreciated!