英文:
Class not found: com.app.handler.AsynchronousLambdaHandler: java.lang.ClassNotFoundException, Current classpath: file:/var/task/
问题
我已经开发了一个Spring Boot REST API,并尝试使用Serverless框架将其部署为API Gateway上的Lambda,但遇到了以下错误:
找不到类:com.app.handler.AsynchronousLambdaHandler:java.lang.ClassNotFoundException
java.lang.ClassNotFoundException:com.app.handler.AsynchronousLambdaHandler。当前类路径:file:/var/task/
我正在使用Spring Boot版本2.7.10,Java版本11,Maven作为构建工具,以及Serverless作为部署工具将应用程序部署到AWS中。
处理程序类位于BOOT-INF/classes/com/app/handler/AsynchronousLambdaHandler.class
,在将应用程序打包为.jar
文件后可用。
serverless.yml
文件如下:
service: integration-service
provider:
name: aws
region: us-east-1
stage: ${opt:stage, 'dev'}
functions:
"integration-service":
handler: com.app.handler.AsynchronousLambdaHandler::handleRequest
name: ${opt:stage, 'dev'}-integration-service
runtime: java11
memorySize: 512
events:
- http: ANY /{proxy+}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>integration-service</artifactId>
<version>1.0.0</version>
<name>integration-service</name>
<description>Integration Service</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-springboot2</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
AsynchronousLambdaHandler.java
文件如下:
package com.app.handler;
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.model.*;
import com.amazonaws.serverless.proxy.spring.*;
import com.amazonaws.services.lambda.runtime.*;
import com.app.IntegrationServiceApplication;
public class AsynchronousLambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
private SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
public AsynchronousLambdaHandler() throws ContainerInitializationException {
handler = (SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse>) new SpringBootProxyHandlerBuilder()
.springBootApplication(IntegrationServiceApplication.class)
.asyncInit()
.buildAndInitialize();
}
@Override
public AwsProxyResponse handleRequest(AwsProxyRequest input, Context context) {
return handler.proxy(input, context);
}
}
我也尝试了许多选项,包括这个(https://stackoverflow.com/questions/72535951/aws-lambda-function-throws-classnotfoundexception-lambdahandler-current-class),但迄今为止都没有运气。
编辑:
已更新pom.xml
以删除Maven插件,以简化问题。
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>integration-service</artifactId>
<version>1.0.0</version>
<name>integration-service</name>
<description>Integration Service</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-springboot2</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
</build>
</project>
英文:
I have developed a spring boot REST API, trying to deploy it using serverless framework as a Lambda on top of API Gateway, and encountered the following the error
Class not found: com.app.handler.AsynchronousLambdaHandler: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.app.handler.AsynchronousLambdaHandler. Current classpath: file:/var/task/
I am using Spring Boot version 2.7.10, java version 11, maven as a build tool, and serverless as a deployment tool to deploy the application in AWS.
The handler class is available at BOOT-INF/classes/com/app/handler/AsynchronousLambdaHandler.class
post-packing the application as a .jar
file.
serverless.yml
service: integration-service
provider:
name: aws
region: us-east-1
stage: ${opt:stage, 'dev'}
functions:
"integration-service":
handler: com.app.handler.AsynchronousLambdaHandler::handleRequest
name: ${opt:stage, 'dev'}-integration-service
runtime: java11
memorySize: 512
events:
- http: ANY /{proxy+}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>integration-service</artifactId>
<version>1.0.0</version>
<name>integration-service</name>
<description>Integration Service</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-springboot2</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
AsynchronousLambdaHandler.java
package com.app.handler;
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.model.*;
import com.amazonaws.serverless.proxy.spring.*;
import com.amazonaws.services.lambda.runtime.*;
import com.app.IntegrationServiceApplication;
public class AsynchronousLambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
private SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
public AsynchronousLambdaHandler() throws ContainerInitializationException {
handler = (SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse>) new SpringBootProxyHandlerBuilder()
.springBootApplication(IntegrationServiceApplication.class)
.asyncInit()
.buildAndInitialize();
}
@Override
public AwsProxyResponse handleRequest(AwsProxyRequest input, Context context) {
return handler.proxy(input, context);
}
}
I have also explored a lot of options including this one (<https://stackoverflow.com/questions/72535951/aws-lambda-function-throws-classnotfoundexception-lambdahandler-current-class>) but no luck so far.
Edit:
Updated the pom.xml to remove maven plugins to simplyfy the problem.
pom.xml (updated)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>integration-service</artifactId>
<version>1.0.0</version>
<name>integration-service</name>
<description>Integration Service</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-springboot2</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
</build>
</project>
答案1
得分: 0
经过查看了许多博客、文档和不同的网页之后,我终于找到了解决方案。正如所有人建议的那样,在pom.xml中添加了以下代码,但大家忽略的最重要的一点是,***不要部署普通的`.jar`文件,而要部署您的`-aws.jar`文件***。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>org.apache.tomcat.embed:*</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<details>
<summary>英文:</summary>
I finally found the solution after going through a lot of blogs, docs, and different web pages. As everyone is suggesting, I have added the following code in pom.xml but the most important point that folks missed highlighting is ***instead of deploying the normal `.jar` file deploy your `-aws.jar` file***.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>org.apache.tomcat.embed:*</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论