为什么在lambda层中调用类文件会引发错误?

问题描述

我正在尝试使用Java实现lambda层。我为lambda层创建了一个项目,代码如下:

代码

  1. pom.xml

    extension UIViewController {
        func setTitle(_ title: String?) {
            self.title = title
            print("title was set to \(title ?? "nil")")
        }
    }
    
    UIViewController().setTitle("other")
    
  2. DataHandler.java

     <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.i3l.layer</groupId>
         <artifactId>customlayer</artifactId>
         <version>1.1.0</version>
     </project>
    

功能代码 我在 pom.xml 添加了层依赖性,例如:

package customlayer;

public class DataHandler {

 public String getData(String input) {
    System.out.println("Inside Layer");
    return "Hello from Layer "+input;
 }
}

LambdaFunctionHandler.java

<dependency>
    <groupId>com.i3l.layer</groupId>
    <artifactId>customlayer</artifactId>
    <version>1.1.0</version>
    <scope>provided</scope>
</dependency>

我尝试将罐子直接上传到图层,然后将罐子放入zip文件,然后将其上传到图层。在这两种情况下,我都会报错:

“ errorMessage”:“ customlayer / DataHandler”,
“ errorType”:“ java.lang.NoClassDefFoundError”,

注意:我已经在lambda函数添加了带有版本号的图层。

如果有人帮我弄清楚为什么在图层中找不到java类的话,那就太好了。我已经从图层下载了代码,并且可以看到导入时类文件的路径正确。

解决方法

这里的问题是目录结构。层依赖项应位于特定目录中,并且随运行时环境而变化。因此,在这种情况下,customlayer-1.1.0.jar应该放在java/lib中,然后将其存档。假设存档名称为customlayer.zip,则结构为

customlayer.zip
└ java/lib/customlayer-1.1.0.jar

此结构将随运行时环境而变化。

有关更多详细信息:https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html