当前位置: 首页 > 知识库问答 >
问题:

AWS Lambda ClassNotFoundException

苗盛
2023-03-14

在dynamoDB更新中调用lambda函数时,我遇到了问题。我已经检查了AWS Lambda:ClassNotFoundException和AWS Lambda NoClassDefFoundError,但没有成功。

我写这个lambda函数调用时,有任何更新的发电机。我按照这个教程。https://docs.aws.amazon.com/lambda/latest/dg/with-dynamodb-create-package.html

所有的事情都已经成功完成,但问题是当我更新Dynamodb并检查AWS云观察的日志时,有一个例外。

Error loading method handleRequest on class com.amazonaws.lambda.demo.LambdaFunctionHandler: java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/amazonaws/services/lambda/runtime/events/DynamodbEvent
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.getMethods(Class.java:1615)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.lambda.runtime.events.DynamodbEvent
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more

我的java代码是。

package com.amazonaws.lambda.demo;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent.DynamodbStreamRecord;


public class LambdaFunctionHandler implements RequestHandler<DynamodbEvent, String> {

    @Override
    public String handleRequest(DynamodbEvent ddbEvent, Context context) {
        // TODO Auto-generated method stub
        for (DynamodbStreamRecord record : ddbEvent.getRecords()){
               System.out.println(record.getEventID());
               System.out.println(record.getEventName());
               System.out.println(record.getDynamodb().toString());

            }
            return "Successfully processed " + ddbEvent.getRecords().size() + " records.";
    }

}

我的pom。xml是

<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>

  <groupId>com.amazonaws.lambda</groupId>
  <artifactId>demo</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
          <forceJavacCompilerUse>true</forceJavacCompilerUse>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
      <createDependencyReducedPom>false</createDependencyReducedPom>
    </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-bom</artifactId>
        <version>1.11.321</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-events</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.1.0</version>
    </dependency>
  </dependencies>
</project>

我将我的处理程序命名为com。亚马逊。拉姆达。演示。lambdFunctionHandler::HandlerRequest

我已经检查了所有的事情都很好,但它仍然给我错误的com.amazonaws.services.lambda.runtime.events.DynamodbEvents这个类是在aws sdk。

共有2个答案

归鹤龄
2023-03-14

如果一切正常,甚至包名称和生成的JAR

而不是maven-shave-plugin,我使用的Spring引导maven-plugin与以下配置。

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot.experimental</groupId>
        <artifactId>spring-boot-thin-layout</artifactId>
        <version>1.0.26.RELEASE</version>
    </dependency>
</dependencies>
<configuration>
    <createDependencyReducedPom>false</createDependencyReducedPom>
    <shadedArtifactAttached>true</shadedArtifactAttached>
    <shadedClassifierName>aws</shadedClassifierName>
    <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/spring.handlers</resource>
        </transformer>
        <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
            <resource>META-INF/spring.factories</resource>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/spring.schemas</resource>
        </transformer>
    </transformers>
</configuration>

然后你就可以跑了

./mvnw clean package

或者如果你正在使用STS/Eclipse,那么运行Maven build...

这将在目标文件夹中生成一个KB的jar文件,您可以使用这个JAR部署在AWS lambda代码上。

附言:

  1. 删除maven shade插件
  2. 我查阅了spring的官方文件。云木卫一
蔚承天
2023-03-14

您必须使用以下命令通过Maven构建您的项目:mvn清洁包。然后转到目标目录,在那里可以找到构建的jar文件。

将jar文件上载到AWS Lambda时,必须上载包含所有依赖项的jar文件(AWS Lambda java events)。请参见屏幕上的示例:

在您的情况下,您必须上传demo-1.0.0。jar而不是original-demo-1.0.0。jar

 类似资料:

相关问答

相关文章

相关阅读