亚马逊网络服务 - API 网关

Posted

技术标签:

【中文标题】亚马逊网络服务 - API 网关【英文标题】:Amazon Web Services - API Gateway 【发布时间】:2017-04-05 03:30:20 【问题描述】:

我正在尝试集成 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 网关, 预期的查询参数是 -

?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

【问题讨论】:

【参考方案1】:

检查您的 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
  

【讨论】:

你能解释一下吗?我也在互联网上发现了这个,这对我来说很奇怪 - #set($keys = []) #foreach($key in $input.params().querystring.keySet()) #set($success = $keys .add($key)) #end #foreach($key in $input.params().headers.keySet()) #if(!$keys.contains($key)) #set($success = $keys. add($key)) #end #end #foreach($key in $input.params().path.keySet()) #if(!$keys.contains($key)) #set($success = $keys .add($key)) #end #end #foreach($key in $keys) "$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", ...

以上是关于亚马逊网络服务 - API 网关的主要内容,如果未能解决你的问题,请参考以下文章

微服务 - 服务网关

对微服务API服务网关的理解

为啥从亚马逊弹性负载均衡器后面发送重定向时收到 502 错误网关?

微服务架构开发实战:API网关意义和常见API网关的实现方式

如何使用API 网关做服务编排?

API网关服务-20190908