2017-08-07 186 views
0

我在嘗試將AWS API網關與AWS lambda函數集成。在我的集成請求中使用「Lambda代理集成」之前,整合工作完美無瑕。無法集成aws lambda的API網關

當我在我的合併請求勾選「使用拉姆達代理集成」,我開始變得:

"Execution failed due to configuration error: Malformed Lambda proxy response"

我用Google搜索了一下週圍,並意識到,我需要以一定的格式發回的響應:

{ 
    "isBase64Encoded": true|false, 
    "statusCode": httpStatusCode, 
    "headers": { "headerName": "headerValue", ... }, 
    "body": "..." 
} 

但是,儘管如此,我仍然繼續看到相同的錯誤。我究竟做錯了什麼?

這是我的lambda表達式是什麼樣子:

@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網關控制檯的API,這是響應我得到:

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\"}"

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}

Mon Aug 07 00:33:45 UTC 2017 : Execution failed due to configuration error: Malformed Lambda proxy response

Mon Aug 07 00:33:45 UTC 2017 : Method completed with status: 502

如果我取消選中'使用Lambda代理集成',一切正常。但我想知道爲什麼我的迴應是畸形的,以及如何解決問題。謝謝!

回答

2

我想通了。我以錯誤的方式發回了回覆。

響應必須作爲POJO對象直接發回,而不是序列化POJO並將其作爲字符串發回。這是我得到它的工作。

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\"}"; 
    } 
} 

希望這可以幫助別人!