簡體   English   中英

What is the easiest way to integrate aws gateway api with Lambda java function and pass json data between them?

[英]What is the easiest way to integrate aws gateway api with Lambda java function and pass json data between them?

My goal is to build a set of REST apis using aws api gateway, back with java Lambda functions and DynamoDB. 請求和響應的負載是 json。 I want to avoid tedious field level mapping code between json and java and between java and dynamodb so am trying to use gson to map json to java objects the enhance dynamodb client so I can use java pojos as inputs and outputs of dynamodb queries. 當單獨測試功能時,這一切都很好。 我遇到的問題是在嘗試將網關 API 集成到功能時,在網關和 Lambda 之間傳遞 json。 我試過Lambda函數的兩個接口RequestHandler和RequestStreamHandler。 我可以每一個都做我需要的一半,但是使用 RequestHander 在將響應正文返回為 json 時出現運行時錯誤,使用 RequestStreamHandler 我可以成功地將響應正文設置為 json,但我在接收請求正文時遇到問題作為 json。 似乎 aws 網關框架中的某些東西正在將主體 json 轉換為轉義字符串,然后框架的另一部分試圖將轉義字符串轉換為 json 並失敗。

我正在嘗試做的似乎是最基本的 REST api 用例,用於 aws 網關和 Lambda,但我在除 trivi 之外的 aws 文檔中找不到任何“hello world”類型的代碼示例。 Aws 文檔似乎也很薄,所以有沒有人成功地使用 aws 網關和 Lambda 構建帶有 json 有效負載的 Z50780F47F47F6839D47D60BC4555EE00C3FZ api 和 Lambda?

這是我嘗試過的一些背景知識:

API 網關:我使用 Lambda 代理集成到我的 Lambda 功能創建了網關 API。 我沒有指定任何模型來驗證輸入。 我剛剛采用了默認設置。 我正在通過 json object 作為我的 HTTP 請求的主體來測試 API。

Lambda使用 SDK 的 V2 在 java 中編碼的函數。

RequestHandler function - I have created a class the implements the RequestHandler interface specifying the input parameter as a HashMap and output as String. 我已經成功地從 hashmap 獲得了 HTTP 請求的正文,並使用 Gson 將正文轉換為我的 ZD523187880E1EA228。 都好。 但是,我未能嘗試將 json 作為響應的主體。 我已經使用了 Gson 和 org.json 來構建我的 http 響應,並將 Z466DEEC76ECDF5FCA6D38571 包括在內。 我可以成功地對 function 進行單元測試,但是當與我的網關 api 集成時,我在網關中收到錯誤(不是響應已被轉義):

2021 年 1 月 12 日星期二 11:12:23 UTC:轉換前的端點響應正文:"{"body":{"message":"hello world"},"isBase64encoded":false,"statusCode":200}" 1 月 12 日星期二11:06:59 UTC 2021:由於配置錯誤,執行失敗:格式錯誤的 Lambda 代理響應

RequestStreamHandler handleRequest 方法的簽名是:

公共無效句柄請求(InputStream 輸入,OutputStream output,上下文上下文)

In this case, I can successfully create a json response using gson and write it to the output stream and the api gateway is fine with it.

但是我的輸入有問題。 我嘗試使用 gson 來解析輸入 stream 並提取正文。 這很好,但正文不是 json,它是一個轉義的 json 字符串,例如:

"{\r\n "id": "10",\r\n "title": "滑雪之旅",\r\n "owner": "captain",\r\n "locations": \r\ n [\r\n {"緯度": 55, "經度":-2 }\r\n ]\r\n}"

我不能使用 Gson 來解析它,也不能使用 Gson 將其轉換為 pojo。 如果我嘗試,那么我會得到運行時異常。 為了解決這個問題,我使用 org.apache.commons.text.StringEscapeUtils 刪除轉義字符,然后刪除字符串周圍的前導和尾隨引號。 然后我可以使用 Gson 將結果字符串轉換為我的 pojo class。 這是極其乏味的。

我嘗試更改用於 handleRequest 方法的輸入和輸出的類,例如返回一個 JsonObject 以查看這是否改變了我收到的內容,但無濟於事。 如果正文只包含一個字符串而不是 json,則沒有問題。 因此,根據我找到的示例,只需將“hello world”作為正文返回即可。

我必須遺漏一些東西,可能在網關 api 的定義中或在 Lambda 函數的輸入類型和 output 參數中,但沒有文檔它真的只是在黑暗中徘徊。 Hey all I want to do is to take a json object from the body of a rest request and write it as a json document to a nosql database, getting tripped up by conversion and mapping is really frustrating.

感謝任何可以幫助我的人。

假設這是我們的 Hander class 與ApiGatewayRequestApiGatewayProxyResponse是請求和響應 class 名稱。

public class MyHandler implements RequestHandler<ApiGatewayRequest, ApiGatewayProxyResponse> {

    @Override
    public ApiGatewayProxyResponse handleRequest(ApiGatewayRequest request, Context context) {    
 }
}

請求 Class

public class ApiGatewayRequest {
    private String body;
    private Map<String, String> headers;
    private Map<String, String> queryStringParameters;
    private Map<String, String> pathParameters;
    private boolean isBase64Encoded;

}

響應 Class

public class ApiGatewayProxyResponse {
    private int statusCode;
    private Map<String, String> headers;
    private String body;
}

Json 字符串應與 com.fasterxml.jackson.databind.ObjectMapper 在body中發送

String body = new ObjectMapper().writeValueAsString(ouputObject)

必要的標頭例如: Access-Control-Allow-CredentialsAccess-Control-Allow-OriginContent-Type

我已經解決了我自己的問題。 In aws gateway I had chosen my api to be of type Rest, which seemed the obvious choice for creating Rest services accessed over HTTP. 我創建了一個類型為 Http 的新 api 以與相同的 Lambda ZC1C425268E68385D41AB5074C1 集成,並解決了長話短說。 The body of my post request as passed to the Lambda function is not escaped and can be process as json, and likewise the body in my response is not escaped or rejected before being returned to an http client. 但是,我通過反復試驗而不是通過遵循清晰的文檔或工作示例發現了這一點。 我的下一步是實現 get、post、put 和 delete HTTP 方法的示例,以了解在路徑變量和查詢字符串的情況下如何傳遞參數,以及完成與 dynamodb 的集成。 完成后,我將發布我的工作代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM