【问题标题】:{ "errorType": "java.lang.ExceptionInInitializerError" } in AWS Lambda Function{ "errorType": "java.lang.ExceptionInInitializerError" } 在 AWS Lambda 函数中
【发布时间】: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


【解决方案1】:

在静态上下文中抛出的异常会导致此错误。

将您的代码更改为以下内容,以便您可以了解为什么它首先会引发异常,然后解决该错误:

. . .
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);
    }
}
. . .

静态加载时要小心。

【讨论】:

  • @sar OP 从未共享堆栈跟踪,因此没有太多机会提供帮助;如果您遇到类似问题,我建议您也打印堆栈跟踪并在新问题中分享。
猜你喜欢
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2023-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多