我正在尝试在 AWS Lambda 中运行 Spring Boot 应用程序(例如,各种示例使用与 https://www.baeldung.com/spring-boot-aws-lambda 基本相同的方法 - 只需复制代码并更改 SpringBoot 应用程序名称),但由于某种原因,当调用函数处理程序时,在 handler.proxy (或 handler.proxyStream)中的某个位置我不断收到:
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
SpringBoot REST 应用程序本身工作正常,但是当我尝试在收到请求时通过函数处理程序运行它时,会返回上述错误代码 415(不支持的媒体类型)。
我的 Java 函数处理程序如下所示(我尝试了两种方法 - 使用 RequestHandler 和 RequestStreamHandler - 两种方法如下所示 - 但都给我带来了相同的内容类型问题):
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() : "";
// }
}
我提供给函数处理程序的测试事件对象包括“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"
}
我还尝试将八位字节流添加到消耗的数据中:
@RestController
@RequestMapping(value = "/data", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public class DataController { ...
即使我只是将函数处理程序部署到 AWS 并尝试通过从 Postman 发送请求(带有简单的 JSON 正文)来触发它(注意,我使用为 AWS 中的 lambda 函数定义的函数 URL 来直接调用它)没有将请求发送到 API 网关),问题保持不变。
我将感激任何人能给我的任何建议......
我也有同样的问题。如果我从 @RequestMapping 中删除“consume”,它就会起作用。但我使用 OpenAPI 生成器,并且“消耗”始终存在。有什么想法如何解决这个问题吗?