Reputation: 996
My team has two AWS Lambdas, written in Golang(1) and Java8(2). Lambda#1 is sending a JSON payload to Lambda#2; due to the nature of Golang in Lambda it must be sent as a byte array. This is where the issues start. I started by declaring my Java handler like so:
public String handleRequest(String input, Context _context) {}
However, before Lambda#2 is even called, Cloudwatch logs indicate a JSON mapping error, saying that START_OBJECT cannot be mapped to String. Very obviously it's attempting to do some mapping of its own before passing it to the Lambda. The next thing i tried was a custom POJO:
public String handleRequest(RequestObj input, Context _context){}
However this also failed, I believe it due to some escaped Strings inside the JSON string. So I guess in all this boils down to 2 questions for me:
According to CW logs, Lambda uses FasterXML's Jackson to map Lambda input. Is there any way to use @JsonProperty
annotations or something similar in a custom RequestObject class? The documentation says:
You shouldn't rely on any other features of serialization frameworks such as annotations.
(But it's still Jackson!!)
Is there a way to tell Lambda to stop trying to Map the input and just take the raw byte array to parse manually? This would solve alot of headaches to be able to do our own custom parsing with annotations.
Thanks in advance, can provide more details if needed.
Upvotes: 2
Views: 5532
Reputation: 17455
I've struggled with the same thing. The solution I've used it to not count on Lambda doing the serialization for me. I do something like:
public void handleRequest(InputStream inputStream,
OutputStream outputStream,
Context context) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(inputStream);
And then do things like:
String name = jsonNode.get("name").asText();
as needed. Of course, you can get your POJO instead if you want - it depends on the size of the object.
I then use jackson-core
in my build file to bring in the needed dependencies. Of course if you need to return anything you can use the ObjectMapper
to write to the OutputStream
.
Upvotes: 7