You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot部署AWS Lambda遇ClassNotFoundException问题求助

解决AWS Lambda找不到Spring Boot Handler类的问题

问题根源

AWS Lambda的Java运行时仅从jar包的根目录加载指定的Handler类,但Spring Boot默认打包的是可执行jar,所有应用类都嵌套在BOOT-INF/classes/目录下,Lambda的类加载器无法识别这种嵌套结构,因此抛出ClassNotFoundException。

解决方案

方案1:用Maven Shade插件打包成扁平结构的jar

修改pom.xml中的Shade插件配置,将BOOT-INF/classes下的类移到jar根目录,并合并必要的元数据文件:

<build>
    <plugins>
        <!-- 保留Spring Boot插件编译,禁用可执行打包 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <finalName>${project.artifactId}</finalName>
                <skip>true</skip>
            </configuration>
        </plugin>
        <!-- 配置Shade插件生成扁平jar -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <transformers>
                            <!-- 把BOOT-INF/classes下的类移到jar根目录 -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <resource>BOOT-INF/classes/</resource>
                                <file>./</file>
                            </transformer>
                            <!-- 合并服务提供者配置文件 -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <!-- 合并Spring组件索引文件 -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.components</resource>
                            </transformer>
                        </transformers>
                        <!-- 排除签名文件减小包体积 -->
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行mvn clean package后,解压生成的jar包,确认com/app/handler/AsynchronousLambdaHandler.class直接位于jar根目录下,而非BOOT-INF/classes/内。

方案2:配置Spring Boot插件生成普通jar(非可执行)

修改Spring Boot插件配置,生成一个专门用于Lambda部署的普通依赖jar:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <finalName>${project.artifactId}</finalName>
                <!-- 生成普通jar,用classifier区分可执行jar -->
                <classifier>lib</classifier>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

打包后会生成两个jar:

  • integration-service.jar:本地运行用的可执行jar
  • integration-service-lib.jar:Lambda部署用的普通jar
    在serverless.yml中指定部署的artifact路径:
package:
  artifact: target/integration-service-lib.jar

方案3:使用Serverless Java Plugin辅助打包

在serverless.yml中添加该插件,自动处理Java Lambda的打包和类路径问题:

plugins:
  - serverless-java-plugin

custom:
  java:
    package:
      artifact: target/integration-service.jar

无需额外调整pom.xml,插件会自动识别Spring Boot的打包结构,确保Lambda能找到Handler类。

验证步骤

  1. 执行mvn clean package后,解压jar包确认Handler类在根目录。
  2. 检查serverless.yml中的handler路径与类全限定名一致:com.app.handler.AsynchronousLambdaHandler::handleRequest。
  3. 重新部署:serverless deploy --stage dev

内容的提问来源于stack exchange,提问作者Tilak Dewangan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 00:48:13