Java:如何从 API 网关获取 AWS Lambda 事件的请求对象?

Java : How to get a Request object for your AWS Lambda event from API Gateway?

有什么方法可以获取 JAVA 中的 lambda 函数的值吗? 我可以看到在 nodejs 中有 event.params 但我无法在 Java.

中找到类似的方法

我可以在 API 网关内容类型中使用它,

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

但是,

我们如何在 Java 中的 AWS Lambda 中获取此输入?

来自 the documentation Java 中的 Lambda 函数处理程序(强调我的):

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.

所以这将在您的 input 参数中可用。

代码示例:复制代码段并自己实现

 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();
 }
}

对于响应对象 JSON 格式遵循 link: https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/