将AWS Header从AWS API传递到Lambda函数

时间:2016-10-15 03:33:17

标签: java amazon-web-services http-headers aws-lambda aws-api-gateway

我尝试使用java在AWS Lambda函数中获取HTTP标头和正文详细信息,这些是使用AWS API通过POST方法发送的。到目前为止,我已成功设置AWS API和AWS Lambda Function之间的连接,并且能够从API网关调用它。现在要将标题传递给Lambda函数,我已经完成了所有的步骤"更新lambda函数"来自here。以下是我的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,例如事件,但它们都不起作用。

2 个答案:

答案 0 :(得分:0)

有效负载映射模板出了问题。在API网关设置期间,您可以指定要将请求映射到事件的方式。您还可以选择一些预先制作的模板。

看看亚马逊的这个文档:

API Gateway API Request and Response Payload-Mapping Template Reference

如果您需要更多帮助,请与我联系。

答案 1 :(得分:0)

handleRequest(InputStream inputStream,OutputStream outputStream,Context context)

我为您提供了一个链接,该链接是从JSON获取标头的正确实现方式。

https://www.baeldung.com/aws-lambda-api-gateway