Class not found: com.app.handler.AsynchronousLambdaHandler: java.lang.ClassNotFoundException, Current classpath: file:/var/task/

huangapple go评论81阅读模式
英文:

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, &#39;dev&#39;}

functions:
    &quot;integration-service&quot;:
        handler: com.app.handler.AsynchronousLambdaHandler::handleRequest
        name: ${opt:stage, &#39;dev&#39;}-integration-service
        runtime: java11
        memorySize: 512
        events:
            -   http: ANY /{proxy+}

pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
		 xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;2.7.10&lt;/version&gt;
		&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
	&lt;/parent&gt;
	&lt;groupId&gt;com.app&lt;/groupId&gt;
	&lt;artifactId&gt;integration-service&lt;/artifactId&gt;
	&lt;version&gt;1.0.0&lt;/version&gt;
	&lt;name&gt;integration-service&lt;/name&gt;
	&lt;description&gt;Integration Service&lt;/description&gt;
	&lt;properties&gt;
		&lt;java.version&gt;11&lt;/java.version&gt;
	&lt;/properties&gt;
	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.amazonaws.serverless&lt;/groupId&gt;
			&lt;artifactId&gt;aws-serverless-java-container-springboot2&lt;/artifactId&gt;
			&lt;version&gt;1.9.2&lt;/version&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;
	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
				&lt;configuration&gt;
					&lt;finalName&gt;${project.artifactId}&lt;/finalName&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
				&lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
				&lt;version&gt;3.2.4&lt;/version&gt;
				&lt;configuration&gt;
					&lt;createDependencyReducedPom&gt;false&lt;/createDependencyReducedPom&gt;
				&lt;/configuration&gt;
				&lt;executions&gt;
					&lt;execution&gt;
						&lt;phase&gt;package&lt;/phase&gt;
						&lt;goals&gt;
							&lt;goal&gt;shade&lt;/goal&gt;
						&lt;/goals&gt;
					&lt;/execution&gt;
				&lt;/executions&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;
&lt;/project&gt;

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&lt;AwsProxyRequest, AwsProxyResponse&gt; {
	private SpringBootLambdaContainerHandler&lt;AwsProxyRequest, AwsProxyResponse&gt; handler;

	public AsynchronousLambdaHandler() throws ContainerInitializationException {
		handler = (SpringBootLambdaContainerHandler&lt;AwsProxyRequest, AwsProxyResponse&gt;) 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)

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
		 xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;2.7.10&lt;/version&gt;
		&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
	&lt;/parent&gt;
	&lt;groupId&gt;com.app&lt;/groupId&gt;
	&lt;artifactId&gt;integration-service&lt;/artifactId&gt;
	&lt;version&gt;1.0.0&lt;/version&gt;
	&lt;name&gt;integration-service&lt;/name&gt;
	&lt;description&gt;Integration Service&lt;/description&gt;
	&lt;properties&gt;
		&lt;java.version&gt;11&lt;/java.version&gt;
	&lt;/properties&gt;
	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.amazonaws.serverless&lt;/groupId&gt;
			&lt;artifactId&gt;aws-serverless-java-container-springboot2&lt;/artifactId&gt;
			&lt;version&gt;1.9.2&lt;/version&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;
	&lt;build&gt;
		&lt;finalName&gt;${artifactId}&lt;/finalName&gt;
	&lt;/build&gt;
&lt;/project&gt;

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



huangapple
  • 本文由 发表于 2023年4月4日 15:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定