我开发了一个spring引导REST API,试图使用无服务器框架作为Lambda部署在API网关之上,并遇到了以下错误
Class not found: com.app.handler.AsynchronousLambdaHandler: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.app.handler.AsynchronousLambdaHandler. Current classpath: file:/var/task/
我正在使用Spring Boot version 2.7.10,java version 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>
<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);
}
}
我还探索了很多选项,包括这个(AWS Lambda函数抛出ClassNotFoundException - LambdaHandler)。当前的类路径:file:/var/task/),但目前还没有找到。
编辑:
更新了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>
在浏览了很多博客、文档和不同的网页后,我终于找到了解决方案。正如大家建议的那样,我在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>