AWSLambda运行SpringBoot时发生NullPointerException
创始人
2024-09-26 10:01:03
0
  1. 确认 Lambda 函数的入口类和 Spring Boot 应用的主类相同,因为 AWS Lambda 会调用入口类的 handleRequest() 方法来处理请求。
  2. 确认 Lambda 函数的依赖项使用和 Spring Boot 应用相同的版本。
  3. 确认 Lambda 函数的运行时配置正确,如内存大小和执行时间等。
  4. 确认 Lambda 函数的执行环境和 Spring Boot 应用的部署环境相同。
  5. 编写 LambdaHandler 类,并在 handleRequest() 方法中调用 Spring Boot 应用的启动方法。
public class LambdaHandler implements RequestHandler {

    private static final SpringApplication application = new SpringApplication(SpringBootLambdaApplication.class);

    private static final ApplicationContextAware contextAware = applicationContext -> {
        LambdaHandler.application.setApplicationContext(applicationContext);
    };

    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
        SpringApplicationBuilder builder = new SpringApplicationBuilder()
                .sources(SpringBootLambdaApplication.class)
                .listeners(contextAware)
                .web(WebApplicationType.NONE);
        try (ConfigurableApplicationContext applicationContext = builder.run()) {
            SpringBootLambdaApplication application = applicationContext.getBean(SpringBootLambdaApplication.class);
            response.setBody(application.handleRequest(input));
        } catch (Exception e) {
            response.setStatusCode(500);
            response.setBody(e.getMessage());
        }
        return response;
    }

}
  1. 修改 build.gradle 文件,以便正确配置 Spring Boot 应用程序的构建和打包,使其可以在 AWS Lambda 上运行。
plugins {
    id 'org.springframework.boot' version '2.3.11.RELEASE' apply false
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:2.3.11.RELEASE'
    implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
    implementation 'com.amazonaws:aws-lambda-java-events:3.7.0'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.4'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.3.11.RELEASE'
}

task buildZip(type: Zip) {
    from compileJava
    from processResources
    from("${

相关内容

热门资讯

揭幕一下!激k辅助器有挂吗,菜... 您好,激k辅助器有挂吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
专业一下!花花生后圈开挂,新九... 专业一下!花花生后圈开挂,新九游辅助软件,其实是真的有挂(哔哩哔哩)1、花花生后圈开挂模拟器是什么优...
解迷一下!途游修改器,开心游戏... 解迷一下!途游修改器,开心游戏快跑辅助,确实是有挂(哔哩哔哩)1、点击下载安装,途游修改器脚本分类一...
解迷一下!点我休闲辅助,新众亿... 解迷一下!点我休闲辅助,新众亿辅助,一贯真的是有挂(哔哩哔哩)点我休闲辅助是不是有人用挂微扑克wpk...
有挂一下!陕西三代实战技巧,老... 您好,陕西三代实战技巧这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
专业一下!丽水茶苑辅助工具,边... 专业一下!丽水茶苑辅助工具,边锋老友修改器,好像真的是有挂(哔哩哔哩)1、很好的工具软件,可以解锁游...
解密一下!钱柜麻将辅助器,九游... 解密一下!钱柜麻将辅助器,九游破解辅助插件,原来是真的有挂(哔哩哔哩)1、解密一下!钱柜麻将辅助器,...
解迷一下!边锋小程序老友辅助,... 解迷一下!边锋小程序老友辅助,随意玩辅助,总是真的有挂(哔哩哔哩)边锋小程序老友辅助破解侠是真的助透...
科普一下!微乐小程序黑科技,欢... 科普一下!微乐小程序黑科技,欢乐达人脚本,都是是有挂(哔哩哔哩)1、不需要AI权限,帮助你快速的进行...
解密一下!微乐陕西三代自建房挂... 解密一下!微乐陕西三代自建房挂,潮汕汇app辅助,一贯是真的有挂(哔哩哔哩)1、点击下载安装,微乐陕...