问题描述
运行lambda函数时出现以下错误,该函数应该将一个项放入DynamoDB表中。
"errorMessage": "com/amazonaws/services/dynamodbv2/AmazonDynamoDBClientBuilder","errorType": "java.lang.NoClassDefFoundError","stackTrace": [
"dao.EventDAO.insertEvent(EventDAO.java:15)","service.PostEventService.postEvent(PostEventService.java:12)","handler.PostEventHandler.handle(PostEventHandler.java:11)","java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)","java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source)","java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source)","java.base/java.lang.reflect.Method.invoke(UnkNown Source)"
]
这是我要插入表中的代码:
package dao;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
import com.amazonaws.services.dynamodbv2.document.Table;
import request.PostEventRequest;
import response.PostEventResponse;
public class EventDAO {
public PostEventResponse insertEvent(PostEventRequest request) {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("Event");
PostEventResponse response = new PostEventResponse();
// Build the item
Item item = new Item()
.withPrimaryKey("eventId",request.eventId)
.withString("username",request.username)
.withString("image",request.image)
.withString("description",request.description)
.withString("time",request.time);
// Write the item to the table
PutItemOutcome outcome = table.putItem(item);
return response;
}
}
这是我的pom.xml,因为我怀疑这可能很重要:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.327</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>earnshawsfinest</groupId>
<artifactId>rally-backend</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
由于某种原因,当它在lambda中时,它无法识别AmazonDynamoDbClientBuilder类,即使在我的IntelliJ项目中它也很重要。我的IntelliJ项目的构建没有问题,并且将我上传到lambda的jar(包含模块和依赖项)导出。如果我取出DynamoDB的东西,则lambda可以正常工作。
我觉得我可能缺少某些地方的下载或导入,但我不知道该怎么办。我已经按照亚马逊的各种教程进行了学习,甚至我以前都做过,但是我没有遇到这个问题。有什么帮助吗?预先感谢!
解决方法
默认情况下,依赖项不捆绑在jar文件中。您需要这样做或在单独的lambda层中提供它们
检查链接Building a deployment package with Maven并查看使用maven-shade-plugin
,该链接应在目标jar文件中构建所有依赖项。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>