我正在尝试集成AWS Api Gateway和AWS Lambda,
我能够调用Lambda函数并获得响应。
但是,当我使用AWS API Gateway并使用GET方法调用Lambda函数时,
我无法使用查询字符串传递参数。
这是我的处理程序-
package example;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.Context;
public class Hello implements RequestHandler<Request, Response>{
public Response myHandler(Request request, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("received a Lambda request");
logger.log("Name param contains: "+request.getName()+" Age param contains: "+request.getAge());
String greetingString = String.format("Hello "+request.getName()+", you are "+request.getAge()+" years old");
return new Response(greetingString);
}
public Response handleRequest(Request arg0, Context arg1) {
// TODO Auto-generated method stub
return null;
}
}
这是Request类-
package example;
public class Request {
String name;
String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
这是Response类-
package example;
public class Response {
String greetings;
public String getGreetings() {
return greetings;
}
public void setGreetings(String greetings) {
this.greetings = greetings;
}
public Response(String greetings) {
this.greetings = greetings;
}
public Response() {
}
}
这是我针对Lambda函数的测试字符串,对此我获得了成功的响应-
{
"name": "Aniruddha",
"age": "25"
}
成功响应Lambda电话-
{
"greetings": "Hello Aniruddha, you are 25 years old"
}
现在是问题所在
我为Get方法配置了一个AWS API Gateway,
预期的查询参数为-
?name=aniruddha&age=25
我得到一个回应机构-
{
"greetings": "Hello null, you are null years old"
}
响应标题-
{"Content-Type":"application/json"}
日志-
Execution log for request test-request
Mon Nov 21 16:28:10 UTC 2016 : Starting execution for request: test-invoke-request
Mon Nov 21 16:28:10 UTC 2016 : HTTP Method: GET, Resource Path: /aniruddhaagw
Mon Nov 21 16:28:10 UTC 2016 : Method request path: {}
Mon Nov 21 16:28:10 UTC 2016 : Method request query string: {name=aniruddha, age=25}
Mon Nov 21 16:28:10 UTC 2016 : Method request headers: {}
Mon Nov 21 16:28:10 UTC 2016 : Method request body before transformations: null
Mon Nov 21 16:28:10 UTC 2016 : Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:520784750730:function:aniruddhaLambda/invocations
Mon Nov 21 16:28:10 UTC 2016 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=**************************************************************************************************************************************************************************************************************************************************************************************dfb8a3, X-Amz-Date=20161121T162810Z, x-amzn-apigateway-api-id=h0b98tua9i, X-Amz-Source-Arn=arn:aws:execute-api:us-east-1:520784750730:h0b98tua9i/null/GET/aniruddhaagw, Accept=application/json, User-Agent=AmazonAPIGateway_h0b98tua9i, Host=lambda.us-east-1.amazonaws.com}
Mon Nov 21 16:28:10 UTC 2016 : Endpoint request body after transformations: null
Mon Nov 21 16:28:10 UTC 2016 : Endpoint response body before transformations: {"greetings":"Hello null, you are null years old"}
Mon Nov 21 16:28:10 UTC 2016 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=7e29d831-b007-11e6-83cc-8b56e673337e, Connection=keep-alive, Content-Length=50, Date=Mon, 21 Nov 2016 16:28:10 GMT, Content-Type=application/json}
Mon Nov 21 16:28:10 UTC 2016 : Method response body after transformations: {"greetings":"Hello null, you are null years old"}
Mon Nov 21 16:28:10 UTC 2016 : Method response headers: {Content-Type=application/json}
Mon Nov 21 16:28:10 UTC 2016 : Successfully completed execution
Mon Nov 21 16:28:10 UTC 2016 : Method completed with status: 200
最佳答案
检查您的API网关请求和响应映射是否正确
参考:http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference
另外,不要忘记部署api更改以使其得到反映
#set($allParams = $input.params())
{
"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
}
}