访问阶段变量 - AWS API网关
问题描述:
我在AWS API GATEWAY下定义了阶段变量。访问阶段变量 - AWS API网关
我想在Scala写的请求处理程序中访问我为这些定义的值。 根据AWS API网关控制台,可以使用$ context对象访问阶段变量。
上下文对象的文档是本here但它没有定义如何使用阶段变量,在这个handleRequest方法。
override def handleRequest(input: java.util.Map[java.lang.String, Object], context: Context): util.Map[String, _] = {
context.getLogger.log("Input: " + input + " \n")
// How do I access the Stage variable here?
}
答
您可以在合并请求部分用身体映射模板和创建像下面的示例一个JSON拿到级可变。
#set($inputRoot = $input.path('$'))
{
"version" : "$stageVariables.version"
}
如果您确定对身体映射模板,请对https://aws.amazon.com/blogs/compute/tag/mapping-templates/
答
Vijayanath的回答一看就是一个简单的方法来实现需要什么。
如果你想这样做的编程方式,而无需到stageVariables
从控制台更改,然后 我会建议使用扬鞭API整合处理阶段变量。
您可以使用JSON模板实现这一这里定义 - AWS labs Swagger Api,并且通过在x-amazon-apigateway-integration
"requestTemplates": {
"application/json": "#set($inputRoot = $input.path('$')) \n{ \n \"version\" : \"$stageVariables.version\" \n}"
}
添加以下行,以便整个JSON文件将是:
"x-amazon-apigateway-auth" : {
"type" : "aws_iam"
},
"x-amazon-apigateway-integration" : {
"type" : "aws",
"uri" : "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:MY_ACCT_ID:function:helloWorld/invocations",
"httpMethod" : "POST",
"credentials" : "arn:aws:iam::MY_ACCT_ID:role/lambda_exec_role",
"requestTemplates": {
"application/json": "#set($inputRoot = $input.path('$')) \n{ \n \"version\" : \"$stageVariables.version\" \n}"
},
"requestParameters" : {
"integration.request.path.integrationPathParam" : "method.request.querystring.latitude",
"integration.request.querystring.integrationQueryParam" : "method.request.querystring.longitude"
},
"cacheNamespace" : "cache-namespace",
"cacheKeyParameters" : [],
"responses" : {
"2\\d{2}" : {
"statusCode" : "200",
"responseParameters" : {
"method.response.header.test-method-response-header" : "integration.response.header.integrationResponseHeaderParam1"
},
"responseTemplates" : {
"application/json" : "json 200 response template",
"application/xml" : "xml 200 response template"
}
},
"default" : {
"statusCode" : "400",
"responseParameters" : {
"method.response.header.test-method-response-header" : "'static value'"
},
"responseTemplates" : {
"application/json" : "json 400 response template",
"application/xml" : "xml 400 response template"
}
}
}
}
您是否尝试过检查'context'对象的内容? – Can
@可以怎么做?我对斯卡拉来说比较新。 – Vaulstein