AWS Lambda 参数传递错误 | API 网关
AWS Lambda parameter passing error | API Gateway
我正在使用一个简单的 AWS Lambda 函数:
public class CreateOrderLambda {
private final ObjectMapper objectMapper = new ObjectMapper();
// This Particular Order needs to be saved to the DynamoDB.
private final DynamoDB dynamoDB = new DynamoDB(AmazonDynamoDBClientBuilder.defaultClient());
public APIGatewayProxyResponseEvent createOrder(final APIGatewayProxyRequestEvent input,
final Context context)
throws JsonProcessingException {
Order thisOrder = objectMapper.readValue(input.getBody(), Order.class);
现在,模板如下所示:-
CreateOrdersFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabspw/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: ordersapi
Handler: com.aditya.learn.function.CreateOrderLambda::createOrder
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref OrdersTable
Events:
OrderEvents:
Type: Api
Properties:
Path: /orders
Method: POST
虽然我通过API网关访问了上述API,但我能够很好地执行请求:-
curl --location --request POST 'https://o2cf5ti7ik.execute-api.us-east-1.amazonaws.com/Prod/orders/' \
--header 'Content-Type: application/json' \
--data-raw '{
"id":1001,
"itemName":"Nariyal",
"quantity":3
}'
但是,在使用以下请求从控制台独立测试 Lambda 时:
{
"id": 1004,
"itemName": "HoneyAlmondFlakes",
"quantity": 7
}
它给出以下错误:-
START RequestId: 63aa8edd-38b3-408e-8727-652393964f14 Version: $LATEST
argument "content" is null: java.lang.IllegalArgumentException
java.lang.IllegalArgumentException: argument "content" is null
at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4757)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3515)
at com.aditya.learn.function.CreateOrderLambda.createOrder(CreateOrderLambda.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
END RequestId: 63aa8edd-38b3-408e-8727-652393964f14
REPORT RequestId: 63aa8edd-38b3-408e-8727-652393964f14 Duration: 4.72 ms Billed Duration: 5 ms Memory Size: 512 MB Max Memory Used: 134 MB
基本上,它无法将请求类型转换为自定义对象。
API0Gateway 是否以某种特殊方式将请求传递给 Lambda?我在独立测试 Lambda 时遗漏了什么?
非常感谢任何帮助。
Marcin 回复的基础:-
尝试使用以下事件结构:-
{
"resource": "/",
"path": "/",
"httpMethod": "POST",
"requestContext": {
"id":7,
"itemName":"Flakes",
"quantity":6,
"resourcePath": "/",
"httpMethod": "POST",
"path": "/Prod/",
"requestId": "JKJaXmPLvHcESHA=",
"time": "23/Jan/2021:05:16:23 +0000"
},
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"Host": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-5e66d96f-7491f09xmpl79d18acf3d050"
},
"multiValueHeaders": {
"accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
],
"accept-encoding": [
"gzip, deflate, br"
]
},
"isBase64Encoded": false
}
仍然是同样的错误:-
START RequestId: 028d4942-f2cf-4dbc-afe3-08783a106555 Version: $LATEST
argument "content" is null: java.lang.IllegalArgumentException
java.lang.IllegalArgumentException: argument "content" is null
at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4757)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3515)
at com.aditya.learn.function.CreateOrderLambda.createOrder(CreateOrderLambda.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
END RequestId: 028d4942-f2cf-4dbc-afe3-08783a106555
REPORT RequestId: 028d4942-f2cf-4dbc-afe3-08783a106555 Duration: 371.97 ms Billed Duration: 372 ms Memory Size: 512 MB Max Memory Used: 134 MB Init Duration: 2568.48 ms
Lambda standalone from console
您从 API 获得的 lambda 函数中的 event
与您 运行 从控制台 函数中使用的 不同。来自 api 的 event
传递给您的函数将有一个 fixed known format。但是当你从控制台 运行 函数时,你传递的 event
格式不正确,因此一切都中断了。
您必须确保 运行 控制台中的代码与 API format 中的 event
结构匹配时使用的 event
结构。
我正在使用一个简单的 AWS Lambda 函数:
public class CreateOrderLambda {
private final ObjectMapper objectMapper = new ObjectMapper();
// This Particular Order needs to be saved to the DynamoDB.
private final DynamoDB dynamoDB = new DynamoDB(AmazonDynamoDBClientBuilder.defaultClient());
public APIGatewayProxyResponseEvent createOrder(final APIGatewayProxyRequestEvent input,
final Context context)
throws JsonProcessingException {
Order thisOrder = objectMapper.readValue(input.getBody(), Order.class);
现在,模板如下所示:-
CreateOrdersFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabspw/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: ordersapi
Handler: com.aditya.learn.function.CreateOrderLambda::createOrder
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref OrdersTable
Events:
OrderEvents:
Type: Api
Properties:
Path: /orders
Method: POST
虽然我通过API网关访问了上述API,但我能够很好地执行请求:-
curl --location --request POST 'https://o2cf5ti7ik.execute-api.us-east-1.amazonaws.com/Prod/orders/' \
--header 'Content-Type: application/json' \
--data-raw '{
"id":1001,
"itemName":"Nariyal",
"quantity":3
}'
但是,在使用以下请求从控制台独立测试 Lambda 时:
{
"id": 1004,
"itemName": "HoneyAlmondFlakes",
"quantity": 7
}
它给出以下错误:-
START RequestId: 63aa8edd-38b3-408e-8727-652393964f14 Version: $LATEST
argument "content" is null: java.lang.IllegalArgumentException
java.lang.IllegalArgumentException: argument "content" is null
at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4757)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3515)
at com.aditya.learn.function.CreateOrderLambda.createOrder(CreateOrderLambda.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
END RequestId: 63aa8edd-38b3-408e-8727-652393964f14
REPORT RequestId: 63aa8edd-38b3-408e-8727-652393964f14 Duration: 4.72 ms Billed Duration: 5 ms Memory Size: 512 MB Max Memory Used: 134 MB
基本上,它无法将请求类型转换为自定义对象。
API0Gateway 是否以某种特殊方式将请求传递给 Lambda?我在独立测试 Lambda 时遗漏了什么?
非常感谢任何帮助。
Marcin 回复的基础:-
尝试使用以下事件结构:-
{
"resource": "/",
"path": "/",
"httpMethod": "POST",
"requestContext": {
"id":7,
"itemName":"Flakes",
"quantity":6,
"resourcePath": "/",
"httpMethod": "POST",
"path": "/Prod/",
"requestId": "JKJaXmPLvHcESHA=",
"time": "23/Jan/2021:05:16:23 +0000"
},
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"Host": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-5e66d96f-7491f09xmpl79d18acf3d050"
},
"multiValueHeaders": {
"accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
],
"accept-encoding": [
"gzip, deflate, br"
]
},
"isBase64Encoded": false
}
仍然是同样的错误:-
START RequestId: 028d4942-f2cf-4dbc-afe3-08783a106555 Version: $LATEST
argument "content" is null: java.lang.IllegalArgumentException
java.lang.IllegalArgumentException: argument "content" is null
at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4757)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3515)
at com.aditya.learn.function.CreateOrderLambda.createOrder(CreateOrderLambda.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
END RequestId: 028d4942-f2cf-4dbc-afe3-08783a106555
REPORT RequestId: 028d4942-f2cf-4dbc-afe3-08783a106555 Duration: 371.97 ms Billed Duration: 372 ms Memory Size: 512 MB Max Memory Used: 134 MB Init Duration: 2568.48 ms
Lambda standalone from console
您从 API 获得的 lambda 函数中的 event
与您 运行 从控制台 函数中使用的 不同。来自 api 的 event
传递给您的函数将有一个 fixed known format。但是当你从控制台 运行 函数时,你传递的 event
格式不正确,因此一切都中断了。
您必须确保 运行 控制台中的代码与 API format 中的 event
结构匹配时使用的 event
结构。