繁体   English   中英

带有 Spring Boot 和 Gradle 的 Aws Lambda 无法正常工作。 调用 AWS Lambda 函数时找不到类异常

[英]Aws Lambda with Spring Boot and Gradle not working. Class not found exception when AWS Lambda function is called

我正在使用 Spring Boot 2.4.8,并为 Aws Lambda 创建了示例演示。 我正在使用构建工具 gradle。 所以当我在 Aws Lambda 上部署我的 jar 时,我收到了 class not found 异常。

供参考,请参阅附件屏幕截图。 在此处输入图像描述

以下是我的 build.gradle 文件

plugins {
    id 'org.springframework.boot' version '2.4.8'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

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

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'com.h2database:h2'
    implementation 'org.springframework.cloud:spring-cloud-function-adapter-aws:2.0.1.RELEASE'
    implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
    implementation 'com.amazonaws:aws-lambda-java-events:2.2.9'
    implementation 'com.google.code.gson:gson:2.8.6'       
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

在 Aws Lambda 上,我在处理程序中配置了以下值。

com.example.handler.NotificationHandler:handleRequest

以下是我的包结构。

在此处输入图像描述

NotificationHandler.java 文件

package com.example.handler;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Component
public class NotificationHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
        response.setIsBase64Encoded(false);
        response.setStatusCode(200);
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        response.setHeaders(headers);
        response.setBody("{\n"
                + "  \"key1\": \"value1\",\n"
                + "  \"key2\": \"value2\",\n"
                + "  \"key3\": \"value3\"\n"
                + "}");
        // log execution details
        Util.logEnvironment(event, context, gson);
        return response;
    }
}

任何帮助或建议对我都有更大的帮助。

Lambda 开发者指南指定了 Gradle 的具体打包说明

task buildZip(type: Zip) {
    from compileJava
    from processResources
    into('lib') {
        from configurations.runtimeClasspath
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM