java - AWS Lambda 获取空值作为查询字符串

标签 java amazon-web-services lambda aws-api-gateway

我已经像这样定义了我的 AWS::ApiGateway::Method

GetOrdersMethod:
        Type: "AWS::ApiGateway::Method"
        Properties:
            ApiKeyRequired: true
            AuthorizationType: "AWS_IAM"
            HttpMethod: "GET"
            RequestParameters:
                method.request.querystring.orderId: false
            ResourceId:
                Ref: "GetOrdersPathResource"
            RestApiId:
                Ref: "GetOrders"
            Integration:
                Type: "AWS_PROXY"
                IntegrationHttpMethod: "POST"
                RequestTemplates:
                    application/json: !Join ["", ["{","\"orderId\": \"$input.params('orderId')\"","}"]]
                Uri: !Join ["", ["arn:aws:apigateway:", !Ref "AWS::Region", ":lambda:path/2015-03-31/functions/",!GetAtt GetProgramsLambdaFunction.Arn, "/invocations"]]

我的处理程序是这样定义的

public class ProgramHandler implements RequestHandler<Request, String>{
    private LambdaLogger logger;

    @Override
    public String handleRequest(Request request, Context context) {
        logger = context.getLogger();

        logger.log("FROM LOGGER: ======= LAMBDA INVOKED ======");
        logger.log("Input value: " +request.getOrderId());
        System.out.println("======= LAMBDA INVOKED ======");
        System.out.println("Input value: " +request.getMarketplaceId());

        return "Lambda Invoked successfully";
    }
}

Request 是一个简单的 java pojo,orderId 只是具有必要的 GETTERS 和 SETTERS 的字段

当我测试 API 网关时,我看到在我的 lambda 日志中,订单 ID 为空。但是,我确实看到它在日志中作为查询字符串传递...在这里

Tue Apr 25 21:57:31 UTC 2017 : Endpoint request body after transformations: {"resource":"/xxxx","path":"/xxxx","httpMethod":"GET","headers":null,"queryStringParameters":{"orderId":"32"},"pathParameters":null,"stageVariables":null,"requestContext":{"accountId":"xxxxxxx","resourceId":"xxxxx","stage":"test-invoke-stage", ... }

为什么我的 handler 中的 orderId 为空?

最佳答案

如果您正在使用 AWS_PROXY 集成,API Gateway 将忽略您的映射模板。 API Gateway 会将此格式的参数发送到您的 Lambda 函数。

{
    "message": "Hello me!",
    "input": {
        "path": "/test/hello",
        "headers": {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate, lzma, sdch, br",
            "Accept-Language": "en-US,en;q=0.8",
            "CloudFront-Forwarded-Proto": "https",
            "CloudFront-Is-Desktop-Viewer": "true",
            "CloudFront-Is-Mobile-Viewer": "false",
            "CloudFront-Is-SmartTV-Viewer": "false",
            "CloudFront-Is-Tablet-Viewer": "false",
            "CloudFront-Viewer-Country": "US",
            "Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
            "Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
            "X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
            "X-Forwarded-For": "192.168.100.1, 192.168.1.1",
            "X-Forwarded-Port": "443",
            "X-Forwarded-Proto": "https"
        },
        "pathParameters": {"proxy": "hello"},
        "requestContext": {
            "accountId": "123456789012",
            "resourceId": "us4z18",
            "stage": "test",
            "requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",
            "identity": {
                "cognitoIdentityPoolId": "",
                "accountId": "",
                "cognitoIdentityId": "",
                "caller": "",
                "apiKey": "",
                "sourceIp": "192.168.100.1",
                "cognitoAuthenticationType": "",
                "cognitoAuthenticationProvider": "",
                "userArn": "",
                "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
                "user": ""
            },
            "resourcePath": "/{proxy+}",
            "httpMethod": "GET",
            "apiId": "wt6mne2s9k"
        },
        "resource": "/{proxy+}",
        "httpMethod": "GET",
        "queryStringParameters": {"name": "me"},
        "stageVariables": {"stageVarName": "stageVarValue"}
    }
}

关于java - AWS Lambda 获取空值作为查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43621860/

相关文章:

c# - 使用 Action 和 Lambda 进行 NUnit 测试

java - 我可以将 HashMap 键和值交换到其他吗?

java - 使用动画更改对象的属性

java - FunctionalInterface Comparator 有 2 个抽象方法

java - 如何使用 lambda 表达式基于一个变量对对象列表进行排序?

amazon-web-services - 在 VPC 中将弹性 IP 分配给自动扩展的 EC2 - AWS

java - 检索 Jaxb 转换的 Enum 类中的 @XmlEnumValue 注释属性值

java - 如何在Java中正确使用MongoDB多线程?

php - 使用多个嵌套 JSON 键进行 DynamoDB 扫描 (PHP)

amazon-web-services - 使用 CLI 将逗号分隔的参数传递给 AWS EMR 中的 spark jar