serverless-java-container
serverless-java-container copied to clipboard
LambdaContainerHandler - Error while mapping object to RequestType class
To help us debug your issue fill in the basic information below using the options provided
Serverless Java Container version: 1.9
Implementations: Spring Boot 2
Framework version: SpringBoot 2.7.6.RELEASE
Frontend service: REST API
Deployment method: Console
Scenario
Describe what you are trying to accomplish
I have created an AP Gateway REST API which proxies to a Springboot Java application exposing differnet endpoints, mostly returning Monos and Fluxes. Payload inputted is below.
{ "resource": "/{proxy+}", "path": "/capitals", "httpMethod": "GET", "headers": { "Accept": "application/json", "Accept-Encoding": "gzip, deflate", "Cache-Control": "no-cache", "Content-Type": "application/json", "Host": "xxxxxxx.execute-api.eu-north-1.amazonaws.com", "Postman-Token": "zzzzzzz-4c21-4a6b-a71a-b5d39ce42332", "User-Agent": "PostmanRuntime/7.28.4", "X-Amzn-Trace-Id": "Root=1-638f4d15-626c445233b14ceb2f567363", "X-BlueCoat-Via": "0drrr83fc54be448", "X-Forwarded-For": "1.2.3.4", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https" }, "multiValueHeaders": { "Accept": [ "application/json" ], "Accept-Encoding": [ "gzip, deflate" ], "Cache-Control": [ "no-cache" ], "Content-Type": [ "application/json" ], "Host": [ "ay9vitrep4.execute-api.eu-north-1.amazonaws.com" ], "Postman-Token": [ "zzzzzzz-4c21-4a6b-a71a-b5d39ce42332" ], "User-Agent": [ "PostmanRuntime/7.28.4" ], "X-Amzn-Trace-Id": [ "Root=1-638f4d15-626c445233b14ceb2f567363" ], "X-BlueCoat-Via": [ "ggg3fc54be448" ], "X-Forwarded-For": [ "1.2.3.4" ], "X-Forwarded-Port": [ "443" ], "X-Forwarded-Proto": [ "https" ] }, "queryStringParameters": null, "multiValueQueryStringParameters": null, "pathParameters": { "proxy": "capitals" }, "stageVariables": null, "requestContext": { "resourceId": "sm5w9j", "resourcePath": "/{proxy+}", "httpMethod": "GET", "extendedRequestId": "cuj7cEsTgi0FkCA=", "requestTime": "06/Dec/2022:14:09:25 +0000", "path": "/dev/capitals", "accountId": "123456789", "protocol": "HTTP/1.1", "stage": "dev", "domainPrefix": "ay9vitrep4", "requestTimeEpoch": 1670335765833, "requestId": "50facc59-b889-44a0-8a58-f86431bf0897", "identity": { "cognitoIdentityPoolId": null, "accountId": null, "cognitoIdentityId": null, "caller": null, "sourceIp": "1.2.3.4", "principalOrgId": null, "accessKey": null, "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": null, "userAgent": "PostmanRuntime/7.28.4", "user": null }, "domainName": "xxxxxxxxx.execute-api.eu-north-1.amazonaws.com", "apiId": "xxxxxxxx" }, "body": null, "isBase64Encoded": false }
Nevertheless, when executing the endpoint, the call fails as seems that above payload cannot be mapped into a RequestType class (which is an AWSProxyRequest)
[main] ERROR com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Error while mapping object to RequestType class | 2022-12-06T15:38:16.811+01:00 | com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
Expected behavior
Describe how you would expect the application to behave Json Payload should be returned with 200 response
Actual behavior
Describe what you are seeing instead Response returns 502 with this error inCloudwatch:
[main] ERROR com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Error while mapping object to RequestType class | 2022-12-06T15:38:16.811+01:00 | com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
Steps to reproduce
Provide code samples we can use to reproduce the issue as part of our integration tests. If there is a public repository for the misbehaving application link to it here
- Added aws-serverless-java-container springboot2 library in pom, version 1.9 2)Added LambdaHandler as below
public class StreamLambdaHandler implements RequestStreamHandler {
private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
// If you are using HTTP APIs with the version 2.0 of the proxy model, use the getHttpApiV2ProxyHandler
// method: handler = SpringBootLambdaContainerHandler.getHttpApiV2ProxyHandler(Application.class);
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring Boot application", e);
}
}
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
handler.proxyStream(inputStream, outputStream, context);
}
}
3)Add typical Application (annotated with @SpringBootpplication) + a RestController with this class:
@RestController
public class Resource {
private AService service;
public Resource1(AService service) {
this.service = service;
}
@GetMapping("/capitals")
public Mono<Capitals> getMono() {
return service.serve()
.collectList()
.map(Capitals::new);
}
@Data
private class Capitals {
final List<String> values;
}
}
Service class:
@Service
public class AService {
public static final List<String> capitals = Arrays.asList("Copenhagen", "Rome", "Berlin", "Reykjavik");
public Flux<String> serve() {
return Flux.fromIterable(capitals);
}
}
```
## Full log output
*Paste the full log output from the Lambda function's CloudWatch logs*
```
<html>
<body>
<!--StartFragment-->
2022-12-06T15:38:16.811+01:00 | 14:38:16.811 [main] ERROR com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Error while mapping object to RequestType class
-- | --
| 2022-12-06T15:38:16.811+01:00 | com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
| 2022-12-06T15:38:16.811+01:00 | at [Source: (ByteArrayInputStream); line: 1, column: 0]
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.ObjectReader._initForReading(ObjectReader.java:359)
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2041)
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1427)
| 2022-12-06T15:38:16.811+01:00 | at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:256)
| 2022-12-06T15:38:16.811+01:00 | at com.nordea.ndf.lambda.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:42)
| 2022-12-06T15:38:16.811+01:00 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| 2022-12-06T15:38:16.811+01:00 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
| 2022-12-06T15:38:16.811+01:00 | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
| 2022-12-06T15:38:16.811+01:00 | at java.base/java.lang.reflect.Method.invoke(Unknown Source)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:375)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:899)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:268)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:206)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.AWSLambda.main(AWSLambda.java:200)
| 2022-12-06T15:38:16.811+01:00 | 14:38:16.811 [main] ERROR com.amazonaws.serverless.proxy.AwsProxyExceptionHandler - Called exception handler for:
| 2022-12-06T15:38:16.811+01:00 | com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
| 2022-12-06T15:38:16.811+01:00 | at [Source: (ByteArrayInputStream); line: 1, column: 0]
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.ObjectReader._initForReading(ObjectReader.java:359)
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2041)
| 2022-12-06T15:38:16.811+01:00 | at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1427)
| 2022-12-06T15:38:16.811+01:00 | at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:256)
| 2022-12-06T15:38:16.811+01:00 | at com.nordea.ndf.lambda.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:42)
| 2022-12-06T15:38:16.811+01:00 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| 2022-12-06T15:38:16.811+01:00 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
| 2022-12-06T15:38:16.811+01:00 | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
| 2022-12-06T15:38:16.811+01:00 | at java.base/java.lang.reflect.Method.invoke(Unknown Source)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:375)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:899)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:268)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:206)
| 2022-12-06T15:38:16.811+01:00 | at lambdainternal.AWSLambda.main(AWSLambda.java:200)
| 2022-12-06T15:38:16.812+01:00 | com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
| 2022-12-06T15:38:16.812+01:00 | at [Source: (ByteArrayInputStream); line: 1, column: 0]
| 2022-12-06T15:38:16.812+01:00 | at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
| 2022-12-06T15:38:16.812+01:00 | at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)
| 2022-12-06T15:38:16.812+01:00 | at com.fasterxml.jackson.databind.ObjectReader._initForReading(ObjectReader.java:359)
| 2022-12-06T15:38:16.812+01:00 | at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2041)
| 2022-12-06T15:38:16.812+01:00 | at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1427)
| 2022-12-06T15:38:16.812+01:00 | at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:256)
| 2022-12-06T15:38:16.812+01:00 | at com.nordea.ndf.lambda.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:42)
| 2022-12-06T15:38:16.812+01:00 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| 2022-12-06T15:38:16.812+01:00 | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
| 2022-12-06T15:38:16.828+01:00 | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
| 2022-12-06T15:38:16.848+01:00 | at java.base/java.lang.reflect.Method.invoke(Unknown Source)
| 2022-12-06T15:38:16.848+01:00 | at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:375)
| 2022-12-06T15:38:16.848+01:00 | at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:899)
| 2022-12-06T15:38:16.848+01:00 | at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:268)
| 2022-12-06T15:38:16.848+01:00 | at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:206)
<!--EndFragment-->
</body>
</html>
```
Can you please increase the log level for Jackson so we get more details?
Tried with different settings but could not increase it more than it is. On a second attempt, I modified the StreamLambdaHandler to use RequestHandler instead of RequestStreamHandler.
This time response was 200, but was empty:
<html>
<body>
<!--StartFragment-->
DEBUG com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletResponse - Response buffer flushed with 0 bytes, latch=1 | 12:13:12.416 [main] DEBUG com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletResponse - Response buffer flushed with 0 bytes, latch=1
-- | --
<!--EndFragment-->
</body>
</html>
//public class StreamLambdaHandler implements RequestStreamHandler {
public class StreamLambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
// If you are using HTTP APIs with the version 2.0 of the proxy model, use the getHttpApiV2ProxyHandler
// method: handler = SpringBootLambdaContainerHandler.getHttpApiV2ProxyHandler(Application.class);
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring Boot application", e);
}
}
/*
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
String text = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
LambdaLogger logger = context.getLogger();
logger.log("IN xxxxxxxxxxxxxxxxxxxxx");
logger.log(text);
logger.log("IN xxxxxxxxxxxxxxxxxxxxx");
logger.log(text.length()+"");
logger.log("IN xxxxxxxxxxxxxxxxxxxxx");
handler.proxyStream(inputStream, outputStream, context);
}*/
@Override
public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("IN xxxxxxxxxxxxxxxxxxxxx");
logger.log(awsProxyRequest.getHttpMethod());
logger.log("IN xxxxxxxxxxxxxxxxxxxxx");
logger.log(awsProxyRequest.getHeaders().toString());
logger.log("IN xxxxxxxxxxxxxxxxxxxxx");
return handler.proxy(awsProxyRequest, context);
}
}
Hmm there may be multiple reasons, hard for me to reproduce with the information given. Would it possible for you to share a repo with a project that I can just deploy to narrow it down? You can find a working Spring Boot 2 sample here: https://github.com/awslabs/aws-serverless-java-container/tree/main/samples/springboot2/pet-store