I'm trying to run a Spring Boot application in AWS Lambda (various examples use basically the same approach as https://www.baeldung.com/spring-boot-aws-lambda, for example - just copy the code and change the SpringBoot application name) but for some reason, when invoking the function handler, somewhere in the handler.proxy (or handler.proxyStream) I keep getting:
Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/octet-stream' is not supported]
c.a.s.p.internal.LambdaContainerHandler : 127.0.0.1 null- null [11/10/2023:13:05:54Z] "POST /data/firstDataCreatedViaSpringBoot null" 415 - "-" "Custom User Agent String" combined
The SpringBoot REST application itself works fine but when I try to run it via the function handler when a request is received, the above error code 415 (Unsupported Media Type) is returned.
My Java function handler looks like this (I've tried two ways - using a RequestHandler and RequestStreamHandler - both approaches are shown below - but both give me the same Content-Type problem):
public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> { //RequestStreamHandler {
private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(DataDemoApplication.class);
} catch (ContainerInitializationException e) {
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring Boot application", e);
}
}
@Override
public AwsProxyResponse handleRequest(AwsProxyRequest input, Context context) { // (InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
// String rawInput = convertStreamToString(inputStream);
// context.getLogger().log("Received input: " + rawInput);
// inputStream = new ByteArrayInputStream(rawInput.getBytes(StandardCharsets.UTF_8));
//
// handler.proxyStream(inputStream, outputStream, context);
return handler.proxy(input, context);
}
// private String convertStreamToString(InputStream is) {
// java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
// return s.hasNext() ? s.next() : "";
// }
}
The test event object I'm providing to the function handler includes "Content-Type": "application/json":
{
"body": "{\"name\": \"Jarda\", \"email\": \"[email protected]\"}",
"resource": "/{proxy+}",
"path": "/data/firstDataCreatedViaSpringBoot",
"httpMethod": "POST",
"pathParameters": {
"proxy": "data/firstDataCreatedViaSpringBoot"
},
"stageVariables": {
"baz": "qux"
},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "en-US,en;q=0.8",
"Cache-Control": "max-age=0",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Content-Type": "application/json",
"Host": "1234567890.execute-api.{dns_suffix}",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Custom User Agent String",
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "zzz",
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"requestContext": {
"accountId": "123456789012",
"resourceId": "123456",
"stage": "prod",
"requestId": "zzz",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"apiKey": null,
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "Custom User Agent String",
"user": null
},
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"apiId": "1234567890"
}
I've also tried to add the octet-stream to the consumed data:
@RestController
@RequestMapping(value = "/data", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public class DataController { ...
Even if I just deploy the function handler to AWS and try to trigger it by sending a request (with a simple JSON body) from Postman (note, I'm using a function URL defined for the lambda function in AWS to call it directly without sending the request to an API gateway), the problem stays the same.
I'll be grateful for any piece of advice anyone can give me...
I have solved it for me: multiValueHeaders
I've added in my Lambda Test the following request:
The following post helped me with this problem: https://github.com/awslabs/aws-serverless-java-container/issues/316#issuecomment-704477159