2016-09-27 101 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類中。