我正在尝试将AWS API Gateway与AWS Lambda函数集成在一起.直到我在集成请求"中使用"Lambda代理集成"之前,该集成才能正常工作.
I am trying to integrate AWS API Gateway with an AWS lambda function. The integration works flawlessly until I use the 'Lambda Proxy integration' in my Integration Request.
当我在集成请求中选中使用Lambda代理集成"时,我开始得到:
When I check 'Use Lambda Proxy integration' in my integration request, I start getting:
由于配置错误,执行失败:Lambda代理格式错误 回应"
"Execution failed due to configuration error: Malformed Lambda proxy response"
我在Google上搜索了一下,意识到我需要以某种格式发送回响应:
I googled around a bit and realized that I need to send back the response in a certain format:
{ "isBase64Encoded": true|false, "statusCode": httpStatusCode, "headers": { "headerName": "headerValue", ... }, "body": "..." }尽管如此,我仍然继续看到相同的错误.我在做什么错了?
However, despite doing that, I still continue to see the same error. What am I doing wrong?
这是我的Lambda函数的样子:
This is what my Lambda function looks like:
@Override public String handleRequest(Object input, Context context) { context.getLogger().log("Input: " + input); return uploadN10KWebsiteRepositoryToS3(); } private String uploadN10KWebsiteRepositoryToS3() { /*BitbucketToS3Upload.JsonResponse jsonResponse = new BitbucketToS3Upload.JsonResponse(); jsonResponse.body = "n10k_website repository uploaded to S3..."; String jsonString = null; try { ObjectMapper mapper = new ObjectMapper(); jsonString = mapper.writeValueAsString(jsonResponse); HashMap<String, Object> test = new HashMap<String, Object>(); test.put("statusCode", 200); test.put("headers", null); test.put("body", "n10k_website repository uploaded to S3"); test.put("isBase64Encoded", false); jsonString = mapper.writeValueAsString(test); } catch (Exception e) { int i = 0; }*/ //return jsonString; return "{\"isBase64Encoded\":false, \"statusCode\":200, \"headers\":null, \"body\": \"n10k_website repository uploaded to S3\"}"; }当我从API Gateway控制台测试API时,这是我得到的响应:
When I test the API from API Gateway console, this is the response I get:
收到的回复.整合延迟:4337 ms星期一八月07 00:33:45 UTC 2017:转换之前的端点响应主体: "{\" isBase64Encoded \:false,\" statusCode \:200,\" headers \:null, \"body \":\"n10k_website存储库已上传到S3 \"}"
Received response. Integration latency: 4337 ms Mon Aug 07 00:33:45 UTC 2017 : Endpoint response body before transformations: "{\"isBase64Encoded\":false, \"statusCode\":200, \"headers\":null, \"body\": \"n10k_website repository uploaded to S3\"}"
UTC 2017年8月7日星期一00:33:45:端点响应标头: {x-amzn-Remapped-Content-Length = 0, x-amzn-RequestId = 0ff74e9d-7b08-11e7-9234-a1a04edc223f, 连接=保持活动状态,内容长度= 121,日期=星期一,2017年8月7日 00:33:45 GMT, X-Amzn-Trace-Id = root = 1-5987b565-7a66a2fd5fe7a5ee14c22633; sampled = 0, Content-Type = application/json}
Mon Aug 07 00:33:45 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=0ff74e9d-7b08-11e7-9234-a1a04edc223f, Connection=keep-alive, Content-Length=121, Date=Mon, 07 Aug 2017 00:33:45 GMT, X-Amzn-Trace-Id=root=1-5987b565-7a66a2fd5fe7a5ee14c22633;sampled=0, Content-Type=application/json}
UTC 2017年8月7日星期一00:33:45:由于配置,执行失败 错误:Lambda代理响应格式错误
Mon Aug 07 00:33:45 UTC 2017 : Execution failed due to configuration error: Malformed Lambda proxy response
2017年8月7日星期一世界标准时间(UTC):方法已完成,状态为:502
Mon Aug 07 00:33:45 UTC 2017 : Method completed with status: 502
如果我取消选中使用Lambda代理集成",则一切正常.但是我想知道为什么我的回复格式错误以及如何解决.谢谢!
If I un-check the 'Use Lambda Proxy integration', everything works okay. But I want to know why my response is malformed and how to fix it. Thanks!
推荐答案我知道了.我以不正确的方式将响应发送回去.
I figured it out. I was sending the response back in an incorrect manner.
必须将响应直接作为POJO对象发送回去,而不是序列化POJO并将其作为字符串发送回去.这就是我的工作方式.
The response had to be sent back as a POJO object directly rather than serializing the POJO and sending it back as a String. This is how I got it to work.
public class BitbucketToS3Upload implements RequestHandler<Object, JsonResponse> { @Data public static class JsonResponse { boolean isBase64Encoded = false; int statusCode = HttpStatus.SC_OK; String headers = null; String body = null; } @Override public JsonResponse handleRequest(Object input, Context context) { context.getLogger().log("Input: " + input); return uploadN10KWebsiteRepositoryToS3(); } private JsonResponse uploadN10KWebsiteRepositoryToS3() { BitbucketToS3Upload.JsonResponse jsonResponse = new BitbucketToS3Upload.JsonResponse(); jsonResponse.body = "n10k_website repository uploaded to S3"; String jsonString = null; try { ObjectMapper mapper = new ObjectMapper(); jsonString = mapper.writeValueAsString(jsonResponse); System.out.println(jsonString); //jsonString = mapper.writeValueAsString(test); } catch (Exception e) { int i = 0; } return jsonResponse; //return "{\"isBase64Encoded\":false, \"statusCode\":200, \"headers\":null, \"body\": \"n10k_website repository uploaded to S3\"}"; } }希望这对某人有帮助!
更多推荐
无法将API网关与AWS Lambda集成
发布评论