gpt4 book ai didi

java - 亚马逊网络服务 - API 网关

转载 作者:行者123 更新时间:2023-11-30 07:02:15 28 4
gpt4 key购买 nike

我正在尝试集成 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"
}

响应 header -

{"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 更改以使其得到反射(reflect)

#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
}
}

关于java - 亚马逊网络服务 - API 网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40725218/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com