[英]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.