Java spring 的 Lambda-云版本到 3.2.3/3.1.7 org/springframework/boot/ApplicationContextFactory:java.lang.NoClassDefFoundError

Java Lambda for spring-cloud version to 3.2.3/3.1.7 org/springframework/boot/ApplicationContextFactory: java.lang.NoClassDefFoundError

我正在尝试将 spring-cloud-function-adapter-aws 的版本从 3.0.7.RELEASE 更改为 3.1.7 或 3.2.3(如 Spring Cloud函数漏洞 CVE-2022-22963) 但由于无法找到 class

而出现错误

java.lang.NoClassDefFoundError: org/spring framework/boot/ApplicationContextFactory 在 org.springframework.cloud.function.context.FunctionalSpringApplication.(FunctionalSpringApplication.java:67) 在 org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.springApplication(AbstractSpringFunctionAdapterInitializer.java:378) 在 org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.initialize(AbstractSpringFunctionAdapterInitializer.java:121) 在 org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler.initialize(SpringBootStreamHandler.java:61) 在 org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler.handleRequest(SpringBootStreamHandler.java:53) 原因:java.lang.ClassNotFoundException:

我的Application.java

@ComponentScan

@SpringBootApplication public class 申请 {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

}

FunctionConfiguration.java

@配置 public class 函数配置 { 私有静态记录器记录器 = LoggerFactory.getLogger(FunctionConfiguration.class);

@Autowired
ActionService service;

@Bean
public Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> containerService() {
    return value -> {
        try {
            APIGatewayProxyResponseEvent responseEvent = checkHttpMethod(value);
            if (responseEvent != null) {
                responseEvent.setBody("Option Method");
                return responseEvent;
            } else {
                return createResponseEvent(value);
            }
        } catch (Exception e) {
            return new APIGatewayProxyResponseEvent().withBody(e.getMessage()).withStatusCode(500)
                    .withHeaders(createResultHeader(value));
        }
    };
}

private APIGatewayProxyResponseEvent checkHttpMethod(APIGatewayProxyRequestEvent event) {
    APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
    if (event.getHttpMethod() != null && event.getHttpMethod().equalsIgnoreCase("options")) {
        responseEvent.setHeaders(createResultHeader(event));
        responseEvent.setStatusCode(200);
        return responseEvent;
    } else
        return null;
}

private APIGatewayProxyResponseEvent createResponseEvent(APIGatewayProxyRequestEvent event) {
    APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();

    try {
        responseEvent = service.actionMethod(event);
        responseEvent.setHeaders(createResultHeader(event));
        return responseEvent;
    } catch (Exception e) {
        logger.error("Error executing method", e);
        responseEvent.setHeaders(createResultHeader(event));
        responseEvent.setBody(e.getMessage());
        responseEvent.setStatusCode(500);
        return responseEvent;
    }

}

private Map<String, String> createResultHeader(APIGatewayProxyRequestEvent event) {
    Map<String, String> resultHeader = new HashMap<>();
    resultHeader.put("Content-Type", "application/json");
    resultHeader.put("Access-Control-Allow-Headers", "Content-Type");
    resultHeader.put("Vary", "Origin");
    
    try {
        String origin = event.getHeaders().get("origin");
        resultHeader.put("Access-Control-Allow-Origin", origin);
    } catch (Exception e) {
        logger.error("origin not exist to add");
    }
    resultHeader.put("Access-Control-Allow-Credentials", "true");
    logger.info(" Headers added ");
    return resultHeader;
}

}

pom.xml

4.0.0 com.app.lambda springBootLambda 1.0.3 springBootLambda

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <aws-lambda-java-core.version>1.2.1</aws-lambda-java-core.version>
    <spring-cloud-function.version>3.0.7.RELEASE
    </spring-cloud-function.version>
    <wrapper.version>1.0.17.RELEASE</wrapper.version>
    <aws-lambda-java-events.version>2.2.7</aws-lambda-java-events.version>
    <aws-java-sdk-s3.version>1.11.792</aws-java-sdk-s3.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-json</artifactId>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-indexer</artifactId>
        <scope>optional</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-function-adapter-aws</artifactId>
        <version>${spring-cloud-function.version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>${aws-lambda-java-core.version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.672</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-core</artifactId>
        <version>1.11.672</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-events</artifactId>
        <version>${aws-lambda-java-events.version}</version>
    </dependency>
</dependencies>
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot.experimental</groupId>
                    <artifactId>spring-boot-thin-layout</artifactId>
                    <version>${wrapper.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <configuration>
                <finalName>${project.artifactId}-${project.version}</finalName>
                <createDependencyReducedPom>false</createDependencyReducedPom>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>shaded</shadedClassifierName>
            </configuration>
        </plugin>
    </plugins>
</build>

serverless.yml

服务:SpringBoot-Lambda 供应商: 名称:aws 运行时间:java8 区域:us-east-1 内存:2048 超时:40

spring:
日本专利局: hibernate.ddl-自动:更新 生成-ddl:真 显示-sql: true

将在 AWS 上上传和执行的 jar 文件

套餐: 工件:target/{project.artifactId}-${project.version}.jar

#define Lambda 函数 职能: 创建方法: 处理程序:org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler 事件:# api 网关 - HTTP: 路径:通过 方法:post 科尔斯:真实 environment: # 环境变量 FUNCTION_NAME:SpringBoot-Lambda

您还需要升级 Spring 引导。您使用的是 2.3.0,在 2.4 中添加了 ApplicationContextFactory,但不再支持 2.4.x。

您应该升级到 Spring Boot 2.5.12 或 2.6.6。