【发布时间】:2020-01-19 18:24:43
【问题描述】:
我在 SpringBoot 中开发了一项服务,并将部署在 AWS 中。在 LambdaHandler 中,Spring Applucation 类名被赋予在 AWS 环境中运行 SpringBoot 应用程序。
但是,当我尝试通过将 JSON 格式的 i/p 作为测试事件进行测试并尝试在调用 Lambda 函数时将记录插入数据库时,在 AWS Lambda 控制台中出现以下错误
{
"errorMessage": "Error loading class com.example.lambda.LambdaHandler",
"errorType": "java.lang.ExceptionInInitializerError"
}
这是我的 LambdaHandler 类
public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(SpringBootApplication.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 AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context) {
return handler.proxy(awsProxyRequest, context);
}
}
这是我的主要应用程序类
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
【问题讨论】:
-
为什么不在
handleRequest方法中初始化handler呢? IE。首先检查handler是否为null,然后初始化它,否则立即使用handler。 `handleRequest(...) { if (handler == null) { handler = ... } // 对 handler 做一些事情 } `
标签: java maven spring-boot aws-lambda