2

Is there any way to get the values in the lambda function which is in JAVA? I can see that there is event.params in nodejs but I can't figure out a similar way in Java.

I can use this in API Gateway Content-Type as,

{
   "name": "$input.params('name')"
}

But,

How can we get this input in AWS Lambda in Java?

2 Answers 2

1

From the documentation for Lambda function handlers in Java (emphasis mine):

The general syntax for the handler is as follows:

outputType handler-name(inputType input, Context context) {
   ...
}

inputType – The first handler parameter is the input to the handler, which can be event data (published by an event source) or custom input that you provide such as a string or any custom data object. In order for AWS Lambda to successfully invoke this handler, the function must be invoked with input data that can be serialized into the data type of the input parameter.

So that would be available in your input parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

How is this serialization done? If I provide {"hi":"bye"} and my Java Object is public class Yolo { private final String hi; public Yolo(String hi) { this.hi = hi; } } will this just work?
My question is answered here: docs.aws.amazon.com/lambda/latest/dg/… - you need to use mutable Objects because Lambda instantiates and then sets. Boo.
0

Code example: Copy the snippet and implement yourself

 import com.amazonaws.services.lambda.runtime.Context;
 import com.amazonaws.services.lambda.runtime.LambdaLogger;
 import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;

 public class ClassName implements RequestStreamHandler {
 @Override
 public void handleRequest(InputStream inputStream, OutputStream outputStream, 
 Context context) throws IOException {
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
     JSONObject response = new JSONObject();
     JSONParser parser = new JSONParser();

     try {
         JSONObject event = (JSONObject) parser.parse(reader);

         //your code here. Use the jsonObject as you wish

         response.put("isBase64Encoded", false);
         response.put("statusCode", 200);
         response.put("headers", headerJson);
         response.put("body", "");
     } catch (Exception ex) {
         ex.printStackTrace();
         response.put("statusCode", int);
         response.put("exception", ex);
     }

     OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
     writer.write(response.toString());
     writer.close();
 }
}

For response object JSON FOrmat follow link: https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.