aws / aws/serverless-java-container

Make fields from com.amazonaws.serverless.proxy.model.AwsProxyRequest optional to set

Offen
#1,495 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Java
Sterne
1.6k
Forks
574
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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"));

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Start with com.amazonaws.serverless.proxy.model.AwsProxyRequest and the validation in AwsProxyHttpServletRequestReader.readRequest. Compare the minimal GET and POST priming examples in the issue with the current request model and context handling. Done means callers can construct the shown requests without dummy fields or objects while valid API Gateway requests continue to work.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
aws, java, spring-boot
Bereich
api, backend
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.