Java Amazon Web服务-API网关

Java Amazon Web服务-API网关,java,amazon-web-services,aws-lambda,aws-api-gateway,Java,Amazon Web Services,Aws Lambda,Aws Api Gateway,我正在尝试集成AWS Api网关和AWS Lambda, 我能够调用Lambda函数并得到响应 但是当我使用AWS API网关并使用GET方法调用Lambda函数时, 我无法使用查询字符串传递参数 这是我的处理程序- package example; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.LambdaLogger; imp

我正在尝试集成AWS Api网关和AWS Lambda,
我能够调用Lambda函数并得到响应
但是当我使用AWS API网关并使用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;
    }
}
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;
    }

}
这是响应类-

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() {
    }

}
{
  "name": "Aniruddha",
  "age": "25"
}
?name=aniruddha&age=25
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
这是Lambda函数的测试字符串,我得到了成功的响应-

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() {
    }

}
{
  "name": "Aniruddha",
  "age": "25"
}
?name=aniruddha&age=25
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
成功响应Lambda呼叫-

{
  "greetings": "Hello Aniruddha, you are 25 years old"
}
{
  "greetings": "Hello null, you are null years old"
}
{"Content-Type":"application/json"}
现在问题来了,
我为Get方法配置了一个awsapi网关,
预期的查询参数为-

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() {
    }

}
{
  "name": "Aniruddha",
  "age": "25"
}
?name=aniruddha&age=25
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
我得到了一个回复主体-

{
  "greetings": "Hello Aniruddha, you are 25 years old"
}
{
  "greetings": "Hello null, you are null years old"
}
{"Content-Type":"application/json"}
响应标题-

{
  "greetings": "Hello Aniruddha, you are 25 years old"
}
{
  "greetings": "Hello null, you are null years old"
}
{"Content-Type":"application/json"}
日志-

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() {
    }

}
{
  "name": "Aniruddha",
  "age": "25"
}
?name=aniruddha&age=25
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网关请求和响应映射是否正确 参考:

另外,不要忘记部署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
  }
}

你能解释一下吗?我也在互联网上找到了这个,这对我来说很奇怪-#set($keys=[])foreach($input.params().querystring.keySet())#set($success=$keys.add($key))#end#foreach($input.params().headers.keySet())#if(!$keys.contains($key))#set($success=$keys.add($key))#end#foreach($input.params())#.path.keySet())#if(!$keys.contains($key))#set($success=$keys.add($key))#end#end{foreach($keys中的key)“$key:“$util.escapeJavaScript($input.params($key))”#if($foreach.hasNext),#end#end}它迭代API网关端点的所有参数,包括路径、查询字符串和标头,然后将其转换为要访问的键值对。处理后,它将类似于{“parameters”:{“path”:{“path_name”:“path_value”,…}“header”:{“header_name”:“header_value”,…}“querystring”:{“querystring_name”:“querystring_value”,…}}