2016-09-27 172 views
0

我使用Lambda - > ApiGateway与Java,我需要阶段变量。AWS ApiGateway请求模板传递

我使用默认的模板,由亚马逊提供的合并请求:

## See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html 
## This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload 
#set($allParams = $input.params()) 
{ 
"body-json" : $input.json('$'), 
"params" : { 
#foreach($type in $allParams.keySet()) 
    #set($params = $allParams.get($type)) 
"$type" : { 
    #foreach($paramName in $params.keySet()) 
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))" 
     #if($foreach.hasNext),#end 
    #end 
} 
    #if($foreach.hasNext),#end 
#end 
}, 
"stage-variables" : { 
#foreach($key in $stageVariables.keySet()) 
"$key" : "$util.escapeJavaScript($stageVariables.get($key))" 
    #if($foreach.hasNext),#end 
#end 
}, 
"context" : { 
    "account-id" : "$context.identity.accountId", 
    "api-id" : "$context.apiId", 
    "api-key" : "$context.identity.apiKey", 
    "authorizer-principal-id" : "$context.authorizer.principalId", 
    "caller" : "$context.identity.caller", 
    "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider", 
    "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType", 
    "cognito-identity-id" : "$context.identity.cognitoIdentityId", 
    "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId", 
    "http-method" : "$context.httpMethod", 
    "stage" : "$context.stage", 
    "source-ip" : "$context.identity.sourceIp", 
    "user" : "$context.identity.user", 
    "user-agent" : "$context.identity.userAgent", 
    "user-arn" : "$context.identity.userArn", 
    "request-id" : "$context.requestId", 
    "resource-id" : "$context.resourceId", 
    "resource-path" : "$context.resourcePath" 
    } 
} 

我切换到新的AWS SDK(包括杰克逊JSON解析现在),并使用这个类来解析模板数据:

https://github.com/ingenieux/lambada/blob/master/lambada-runtime/src/main/java/io/ingenieux/lambada/runtime/model/PassthroughRequest.java

我的问题是现在,我创建一个Unittest并让他们解析一个例子。一切工作正常。但是当它在AWS中运行时,阶段变量未被填充。

如果我在模板中将“stage-variables”更改为“stageVariables”,并调整java类......它是正确填充的。

为什么它不在属性名称中使用“ - ”?

感谢

回答

0

我无法从看您提供的模板类源诊断问题。

您可以在API上启用CloudWatch日志并运行测试。这应该允许您查看将API网关传递给Lambda的情况,并让我们确定问题是在模板还是Java类中。