java 通过 Amazon API Gateway 将 POST 请求正文传递给 Lambda

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44781845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:20:54  来源:igfitidea点击:

passing POST request body through Amazon API Gateway to Lambda

javajsonamazon-web-servicesaws-lambdaaws-api-gateway

提问by GameDroids

I have a AWS Lambda function written in Java, that gets triggered by an AWS API Gateway call.

我有一个用 Java 编写的 AWS Lambda 函数,它由 AWS API Gateway 调用触发。

I am trying to make a POSTrequest to one of the endpoints with a JSON as payload.

我正在尝试POST使用 JSON 作为有效负载向其中一个端点发出请求。

curl -H "Content-Type: application/json" -X POST -d '{"firstName":"Mr", "lastName":"Awesome"}' https://someexample.execute-api.eu-central-1.amazonaws.com/beta/MethodHandlerLambda

The gateway will then detectthe Content-Typeand pass all request parameters (including the body) through a default template. The interesting part is this one

然后网关将检测Content-Type,并通过一个缺省传递所有请求参数(包括人体)的模板。有趣的部分是这个

#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
 ....

It is supposed to present me with a Map<String, Object>that is passed to my Java method:

它应该向我展示Map<String, Object>传递给我的 Java 方法的一个:

public void myHandler(Map<String, Object> input, Context context){
    input.keySet().forEach((key) -> {
        System.out.println(key + " : " + input.get(key));
    });
}

And the result shouldbe something like this:

结果应该是这样的:

body-json : {"firstName":"Mr", "lastName":"Awesome"}
...

But what I am getting is this:

但我得到的是:

body-json : {firstName=Mr, lastName=Awesome}

Another possibility would be to pass the whole body as string:

另一种可能性是将整个主体作为字符串传递:

"body" : $input.body

but that again just "converts" to key=valueinstead of key:value

但这又只是“转换”为key=value而不是key:value

How do I have to configure the template to simply pass me the body so I can use it in a JSON parser?

我如何配置模板以简单地将主体传递给我,以便我可以在 JSON 解析器中使用它?

采纳答案by GameDroids

And again - just posting a question here on SO helps to find the answer on your own :)

再一次 - 只是在 SO 上发布一个问题有助于您自己找到答案:)

In the AWS Api Gateway template I set the body to

在 AWS Api Gateway 模板中,我将正文设置为

"body-json" : $input.body

which shouldreturn the complete payload as a String.

应该将完整的有效负载作为字符串返回。

But more importantly I read Greggs answer to his own questionand changed my methodto

但更重要的是,我阅读了Greggs 对他自己问题的回答并将我的方法改为

public void myHandler(InputStream inputStream, OutputStream outputStream, Context context) throws IOException{
    final ObjectMapper objectMapper = new ObjectMapper();
    JsonNode json = objectMapper.readTree(inputStream);
    System.out.println(json.toString());        
}

So, it is sufficient to have a simple InputStreamand read it as a JsonNodewith whatever JSON library one prefers (I am using Hymanson FasterXML). And voila, it packs all possible parameters in a single JSON (as specified by the template)

因此,拥有一个简单的InputStream并将其作为JsonNode一个喜欢的任何 JSON 库阅读就足够了(我使用的是 Hymanson FasterXML)。瞧,它将所有可能的参数打包在一个 JSON 中(由模板指定)

{
    "body-json": {
        "firstName": "Mr",
        "lastName": "Awesome"
    },
    "params": {
       ...
    },
    "stage-variables": {
       ...
    },
    "context": {
        ...
    }
}