Description
Hi Stefano, As per our conversation over the chime, here is the Issue for the NPE
- Framework version: 1.3
- Implementations: Spring Boot
Scenario
I was recently trying out to develop a microservice using Java based lambda using spring boot. After following all the instruction, my requests continuously failed giving NullPointerException while testing the lambda endpoint using Test console of API Gateway. The API Gateway is using Lambda Proxy integration to route the endpoint params to lambda.
Expected behavior
It shouldnt give NPE
Actual behavior
Giving below exception
java.lang.NullPointerException
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:39)
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:28)
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:174)
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:209)
at com.amazon.sa.SpringLambdaHandler.handleRequest(SpringLambdaHandler.java:29)
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)
at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:350)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293)
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:104)
END RequestId: c6ca76ec-db10-454a-8ffe-449b84c3ac74
Steps to reproduce
- create a lambda spring boot handler as per the given instructions in this guide.
- Expose this function to API Gateway using Lambda Proxy Integration
- Test the API from API Gateway console
- Keep the Headers empty as default
- Get above NPE
Full log output
After reviewing your code of AwsProxyHttpServletRequestReader.java, I think as a best practice, we should write NPE free code as line below in highlighted in proned to NPE in case the Content-type header is not sent in the request. Although very rare to be null but still to have check on NPE in the code and avoid failing the requests.
if (request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE) != null) {
String contentType = request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
// put single as we always expect to have one and only one content type in a request.
request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_TYPE, getContentTypeWithCharset(contentType, config));
}