aws / aws/serverless-java-container
Make fields from com.amazonaws.serverless.proxy.model.AwsProxyRequest optional to set
- 主要语言
- Java
- 星标
- 1.6k
- 派生
- 574
- PR 合并指标
- 30 天内没有已合并 PR
描述
When using AWS Serverless Java Container with Spring Boot 3 and Lambda SnapStart with priming with I tested this type of priming implementation [WebRequestPriming](https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/blob/master/spring-boot-3.4-with-aws-serverless-java-container/src/main/java/software/amazonaws/example/product/handler/StreamLambdaHandlerWithWebRequestPriming.javal)
Unfortunately I have to set so many fields of the com.amazonaws.serverless.proxy.model.AwsProxyRequest with dummy values and even create dummy objects like AwsProxyRequestContext and ApiGatewayRequestIdentity (see the method **getAwsProxyRequest**) in order not to get errors like:
at
com.amazonaws.serverless.exceptions.InvalidRequestEventException: The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:48)
or
java.lang.NullPointerException: Cannot invoke "com.amazonaws.serverless.proxy.model.ApiGatewayRequestIdentity.getAccessKey()" because the return value of "com.amazonaws.serverless.proxy.model.AwsProxyRequestContext.getIdentity()" is null
Here is the complete code I had to write in order to make it work:
private static AwsProxyRequest getAwsProxyRequest () {
final AwsProxyRequest awsProxyRequest = new AwsProxyRequest ();
awsProxyRequest.setHttpMethod("GET");
awsProxyRequest.setPath("/products/0");
//awsProxyRequest.setResource("/products/{id}");
awsProxyRequest.setPathParameters(Map.of("id","0"));
final AwsProxyRequestContext awsProxyRequestContext = new AwsProxyRequestContext();
final ApiGatewayRequestIdentity apiGatewayRequestIdentity= new ApiGatewayRequestIdentity();
apiGatewayRequestIdentity.setApiKey("blabla");
awsProxyRequestContext.setIdentity(apiGatewayRequestIdentity);
awsProxyRequest.setRequestContext(awsProxyRequestContext);
return awsProxyRequest;
}
For my use case this will be enough to do the priming like this:
final AwsProxyRequest awsProxyRequest = new AwsProxyRequest ();
awsProxyRequest.setHttpMethod("GET");
awsProxyRequest.setPathParameters(Map.of("id","0"));
Others, that priming Post request would only like to set the body like this:
final AwsProxyRequest awsProxyRequest = new AwsProxyRequest ();
awsProxyRequest.setHttpMethod("POST");
awsProxyRequest.setBody("bla");
Many field should be optional to set to easily construct such objects or please provide static factory methods for it to save on the boilerplate code.
Compare it with how easy it is to implement the same type of SnapStart priming with
1) Spring Cloud Function AWS Adapter
see my implementation of the method **getAPIGatewayProxyRequestEventAsJson** here [AmazonAPIGatewayPrimingResource](https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/blob/master/spring-boot-3.4-with-spring-cloud-function/src/main/java/software/amazonaws/example/product/handler/GetProductByIdWithWebRequestPrimingHandler.javal)
as I only do
final APIGatewayProxyRequestEvent proxyRequestEvent = new APIGatewayProxyRequestEvent ();
proxyRequestEvent.setHttpMethod("GET");
proxyRequestEvent.setPathParameters(Map.of("id","0"));
2) Quarkus 3, see my implementation of the method **getAwsProxyRequest** here [AmazonAPIGatewayPrimingResource](https://github.com/Vadym79/AWSLambdaJavaWithQuarkus/blob/main/quarkus-3.18/src/main/java/software/amazonaws/example/product/handler/AmazonAPIGatewayPrimingResource.javal)
as I only do the same as above:
final APIGatewayProxyRequestEvent proxyRequestEvent = new APIGatewayProxyRequestEvent ();
proxyRequestEvent.setHttpMethod("GET");
proxyRequestEvent.setPathParameters(Map.of("id","0"));
贡献指南
调研方向
从 com.amazonaws.serverless.proxy.model.AwsProxyRequest 以及 AwsProxyHttpServletRequestReader.readRequest 中的验证开始。将 issue 中最小的 GET 和 POST 初始化示例与当前的 request model 和 context 处理进行比较。当调用方无需 dummy 字段或对象即可构造所示 requests,同时有效的 API Gateway requests 仍能正常工作时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- aws, java, spring-boot
- 领域
- api, backend
- Issue 类型
- 功能
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100