您好,我正在尝试将我的 Spring Boot 应用程序部署到 aws lambda。这发生在我的主仓库中,我剥离到非常基本的设置仍然给出了 ClassCastException。我尝试在本地运行该 jar 来重现。下面是堆栈跟踪
20:47:59.921 [main] INFO com.amazonaws.serverless.proxy.spring.AwsSpringWebRuntimeInitializer -- AWS Environment: {PATH=/Users/gowdagp/anaconda3/condabin:/Users/gowdagp/Desktop/Autodesk/vcpkg:/opt/homebrew/bin:/opt/homebrew/sbin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/go/bin:/Users/gowdagp/.npm-global/lib/node_modules:/Users/gowdagp/.npm-global/lib/node_modules/yo, MANPATH=/opt/homebrew/share/man::, IJ_RESTARTER_LOG=/Users/gowdagp/Library/Logs/JetBrains/IntelliJIdea2023.3/restarter.log, CONDA_EXE=/Users/gowdagp/anaconda3/bin/conda, JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home, CONDA_PYTHON_EXE=/Users/gowdagp/anaconda3/bin/python, TERM=xterm-256color, HOMEBREW_PREFIX=/opt/homebrew, COMMAND_MODE=unix2003, artifactory_username=gowdagp, _CE_M=, LOGNAME=gowdagp, HOMEBREW_REPOSITORY=/opt/homebrew, PWD=/Users/gowdagp/Desktop/Personal/FusionDebugging, XPC_SERVICE_NAME=0, JAVA_MAIN_CLASS_8417=com.example.DebuggingApplication, CONDA_SHLVL=0, INFOPATH=/opt/homebrew/share/info:, _=/usr/bin/java, __CFBundleIdentifier=com.jetbrains.intellij, SHELL=/bin/zsh, HOMEBREW_CELLAR=/opt/homebrew/Cellar, OLDPWD=/Users/gowdagp/Desktop/Personal/FusionDebugging, USER=gowdagp, VCPKG_ROOT=/Users/gowdagp/Desktop/Autodesk/vcpkg, TERMINAL_EMULATOR=JetBrains-JediTerm, TMPDIR=/var/folders/8w/733prh7x7m3bh36cnvf_jqvw0000gp/T/, SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.rK4L8CgsUE/Listeners, _CE_CONDA=, XPC_FLAGS=0x0, TERM_SESSION_ID=7bacf5ce-24d6-441d-8be5-1c97a5c5b663, artifactory_password=Pg2451999845#01234567, __CF_USER_TEXT_ENCODING=0x1F6:0x0:0x0, LC_CTYPE=UTF-8, IDEA_INITIAL_DIRECTORY=/, SHLVL=1, HOME=/Users/gowdagp}
20:47:59.927 [main] INFO com.example.DebuggingApplication -- Starting DebuggingApplication using Java 17.0.10 with PID 8417 (/Users/gowdagp/Desktop/Personal/FusionDebugging/build/libs/debugging-service-0.0.1-SNAPSHOT-all.jar started by gowdagp in /Users/gowdagp/Desktop/Personal/FusionDebugging)
20:47:59.927 [main] INFO com.example.DebuggingApplication -- No active profile set, falling back to 1 default profile: "default"
20:48:00.035 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext -- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletWebServerFactory' defined in class path resource [org/springframework/cloud/function/serverless/web/ServerlessAutoConfiguration.class]: class org.springframework.context.annotation.AnnotationConfigApplicationContext cannot be cast to class org.springframework.boot.web.context.ConfigurableWebServerApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext and org.springframework.boot.web.context.ConfigurableWebServerApplicationContext are in unnamed module of loader 'app')
20:48:00.036 [main] ERROR org.springframework.boot.SpringApplication -- Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletWebServerFactory' defined in class path resource [org/springframework/cloud/function/serverless/web/ServerlessAutoConfiguration.class]: class org.springframework.context.annotation.AnnotationConfigApplicationContext cannot be cast to class org.springframework.boot.web.context.ConfigurableWebServerApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext and org.springframework.boot.web.context.ConfigurableWebServerApplicationContext are in unnamed module of loader 'app')
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:607)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:334)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
at com.example.DebuggingApplication.main(DebuggingApplication.java:10)
Caused by: java.lang.ClassCastException: class org.springframework.context.annotation.AnnotationConfigApplicationContext cannot be cast to class org.springframework.boot.web.context.ConfigurableWebServerApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext and org.springframework.boot.web.context.ConfigurableWebServerApplicationContext are in unnamed module of loader 'app')
at org.springframework.cloud.function.serverless.web.ServerlessAutoConfiguration$ServerlessServletWebServerFactory.setApplicationContext(ServerlessAutoConfiguration.java:91)
at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:110)
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:85)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600)
... 14 common frames omitted
`
我尝试使用 SpringBootProxyHandlerBuilder 还是一样
handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
.defaultProxy()
.springBootApplication(DebuggingApplication.class)
.buildAndInitialize();
```
Below are the code snippets
DebuggingApplication
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DebuggingApplication {
public static void main(String[] args) {
SpringApplication.run(DebuggingApplication.class, args);
}
}
StreamLambdaHandler
package com.example;
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamLambdaHandler implements RequestStreamHandler {
private static final SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler= SpringBootLambdaContainerHandler.getAwsProxyHandler(DebuggingApplication.class);
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring Boot application", e);
}
}
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
handler.proxyStream(inputStream, outputStream, context);
}
}
buil.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.4'
id 'io.spring.dependency-management' version '1.1.4'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
springBoot {
mainClass.set('com.example.DebuggingApplication')
}
apply from: 'dependencies.gradle'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.amazonaws.serverless:aws-serverless-java-container-springboot3:2.0.1'
}
shadowJar {
archiveBaseName.set('debugging-service')
archiveVersion.set('0.0.1-SNAPSHOT')
manifest {
attributes 'Main-Class': 'com.example.DebuggingApplication'
}
}
此问题是由 Spring Cloud Function 的传递依赖引起的,已在 https://github.com/spring-cloud/spring-cloud-function/pull/1169 中修复。您可以排除该依赖项,直到发布新版本。另请参阅:https://github.com/aws/serverless-java-container/issues/904