AWS Lambda函数中的{“errorType”:“java.lang.ExceptionInInitializerError”}
我在SpringBoot中开发了一个服务,并将在AWS中部署。在LambdaHandler中,Spring应用程序类名用于在AWS环境中运行SpringBoot应用程序 但是,当我试图通过将JSON格式的I/p作为测试事件进行测试,并试图在调用Lambda函数时将记录插入DB时,我在AWS Lambda控制台中遇到了以下错误AWS Lambda函数中的{“errorType”:“java.lang.ExceptionInInitializerError”},java,maven,spring-boot,aws-lambda,Java,Maven,Spring Boot,Aws Lambda,我在SpringBoot中开发了一个服务,并将在AWS中部署。在LambdaHandler中,Spring应用程序类名用于在AWS环境中运行SpringBoot应用程序 但是,当我试图通过将JSON格式的I/p作为测试事件进行测试,并试图在调用Lambda函数时将记录插入DB时,我在AWS Lambda控制台中遇到了以下错误 { "errorMessage": "Error loading class com.example.lambda.LambdaHandler", "errorTy
{
"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);
}
}
在静态上下文中引发的异常会导致此错误 将您的代码更改为以下内容,以便您首先了解它引发异常的原因,然后解决该错误:
. . .
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(SpringBootApplication.class);
} catch (Exception 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);
}
}
. . .
静态加载时一定要小心。为什么不在
handleRequest
方法中初始化处理程序?也就是说,首先检查handler
是否为空,然后初始化它,否则立即使用handler
HandlerRequest(…){if(handler==null){handler=…}//使用handler做点什么}`
. . .
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(SpringBootApplication.class);
} catch (Exception 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);
}
}
. . .