gpt4 book ai didi

java - 将 HTTP header 从 AWS API 传递到 Lambda 函数

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:59 30 4
gpt4 key购买 nike

我正在尝试使用 java 获取 AWS Lambda 函数中的 HTTP header 和正文详细信息,这些信息是使用 AWS API 通过 POST 方法发送的。到目前为止,我已经成功设置了 AWS API 和 AWS Lambda Function 之间的连接,并且能够从 API Gateway 调用它。现在,要将 header 传递给 Lambda 函数,我已完成 here 中的“更新 lambda 函数”之前的所有步骤。 。以下是我的 lambda 代码:

    import com.amazonaws.Request;
import com.amazonaws.Response;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.S3Event;

public class SimpleSysout implements RequestHandler<Request, String> {
public String handleRequest(Request request, Context context) {
System.out.println("Method = "+request.getHttpMethod());
System.out.println("Headers= "+request.getHeaders().toString());
System.out.println("Content= "+request.getContent().toString());
System.out.println("Servcie Name = "+request.getServiceName().toString());
System.out.println("Resource Path = "+request.getResourcePath().toString());
return request.toString();
}

}

但是我收到了类似的错误

    {
"errorMessage": "An error occurred during JSON parsing",
"errorType": "java.lang.RuntimeException",
"stackTrace": [],
"cause": {
"errorMessage": "com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.amazonaws.Request, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@4dfa3a9d; line: 1, column: 1]",
"errorType": "java.io.UncheckedIOException",
"stackTrace": [],
"cause": {
"errorMessage": "Can not construct instance of com.amazonaws.Request, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@4dfa3a9d; line: 1, column: 1]",
"errorType": "com.fasterxml.jackson.databind.JsonMappingException",
"stackTrace": [
"com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)",
"com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:889)",
"com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:139)",
"com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1511)",
"com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1102)"
]
}
}
}

我也尝试过使用不同的参数来代替Request,例如事件,但它们都不起作用。

最佳答案

您的有效负载映射模板有问题。在 API 网关设置期间,您可以指定如何将请求映射到事件。还有一些预制模板可供您选择。

看看亚马逊的这份文档:

API Gateway API Request and Response Payload-Mapping Template Reference

如果您需要更多帮助,请告诉我。

关于java - 将 HTTP header 从 AWS API 传递到 Lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40054659/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com