我已从 Spring boot 3.3.0 -> spring boot 3.4.0 升级了 java azure 函数依赖项,并使用以下依赖项。
com.microsoft.azure.functions:azure-functions-java-library:3.1.0
org.springframework.cloud:spring-cloud-function-dependencies:4.2.0
com.google.code.gson:gson:2.8.9
现在,我开始发现问题。我尝试升级 gson 版本 2.11.0/2.10.1 甚至删除它,但我仍然收到以下错误。
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gsonBuilder' defined in class path resource [org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.class]: Failed to instantiate [com.google.gson.GsonBuilder]: Factory method 'gsonBuilder' threw exception with message: 'com.google.gson.GsonBuilder com.google.gson.GsonBuilder.setStrictness(com.google.gson.Strictness)'
在下面添加更多错误详细信息:
Description:
[2024-12-19T17:32:38.580Z] An attempt was made to call a method that does not exist. The attempt was made from the following location:
[2024-12-19T17:32:38.583Z] org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration$StandardGsonBuilderCustomizer.customize(GsonAutoConfiguration.java:95)
[2024-12-19T17:32:38.586Z] The following method did not exist:
[2024-12-19T17:32:38.589Z] 'com.google.gson.GsonBuilder com.google.gson.GsonBuilder.setStrictness(com.google.gson.Strictness)'
[2024-12-19T17:32:38.594Z] The calling method's class, org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration$StandardGsonBuilderCustomizer,
was loaded from the following location:
[2024-12-19T17:32:38.600Z] jar:file:/C:/workingdirectory/projectname/target/azure-functions
/projectname/lib/spring-boot-autoconfigure-3.4.0.jar!/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration$StandardGsonBuilderCustomizer.class
[2024-12-19T17:32:38.603Z] The called method's class, com.google.gson.GsonBuilder, is available from the following locations:
[2024-12-19T17:32:38.610Z] jar:file:/C:/Program%20Files/Microsoft/Azure%20Functions%20Core%20Tools/workers/java/azure-functions-java-worker.jar!/com/google/gson/GsonBuilder.class
[2024-12-19T17:32:38.613Z] jar:file:/C:/<directory>/target/azure-functions/projectname/lib/gson-2.11.0.jar!/com/google/gson/GsonBuilder.class
[2024-12-19T17:32:38.617Z] The called method's class hierarchy was loaded from the following locations:
[2024-12-19T17:32:38.621Z] com.google.gson.GsonBuilder: file:/C:/Program%20Files/Microsoft/Azure%20Functions%20Core%20Tools/workers/java/azure-functions-java-worker.jar
[2024-12-19T17:32:38.625Z] Action:
[2024-12-19T17:32:38.629Z] Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.boot.autoconfigure.gson.
GsonAutoConfiguration$StandardGsonBuilderCustomizer and com.google.gson.GsonBuilder.
与 Spring Boot 3.4.0 、 Spring Framework 6.2.0 和 Azure Functions Java Library 3.1.0 一起使用的正确 GSON 版本是什么?
提前感谢您的帮助:)。
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gsonBuilder' defined in class path resource [org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.class]: Failed to instantiate [com.google.gson.GsonBuilder]: Factory method 'gsonBuilder' threw exception with message: 'com.google.gson.GsonBuilder com.google.gson.GsonBuilder.setStrictness(com.google.gson.Strictness)'
该问题可能是由于您使用的依赖项版本之间存在冲突造成的。
感谢您的见解@E.Makarovas。
Spring Boot 将正确的
gson
jar 配置为依赖项,它将自动包含在您的构建中。当您显式指定对 gson
的依赖时,它将覆盖 Spring Boot 带来的默认依赖版本。
要解决此错误:
gson
的显式依赖(或)build.gradle
。com.google.code.gson:gson
更新为 2.11.0
。com.microsoft.azure.functions:azure-functions-java-library:3.0.0
org.springframework.cloud:spring-cloud-function-dependencies:4.0.0
com.google.code.gson:gson:2.11.0
Gson
类的依赖项或排除 Gson
类的“AutoConfiguration”:@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
代码片段:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(DemoApplication.class, args);
}
}
我使用最新版本的库创建了一个 Spring Boot Azure 函数。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-dependencies</artifactId>
<version>${spring.cloud.function.dependencies}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure.functions</groupId>
<artifactId>azure-functions-java-library</artifactId>
<version>${azure.functions.java.library.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
您可以在下面的控制台输出中检查已安装的依赖版本:
控制台输出:
Azure Functions Core Tools
Core Tools Version: 4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875
[2024-12-20T06:17:24.943Z] OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
Functions:
hello: [GET,POST] http://localhost:7071/api/hello
For detailed output, run func with --verbose flag.
[2024-12-20T06:17:26.572Z] Worker process started and initialized.
[2024-12-20T06:17:29.679Z] Host lock lease acquired by instance ID '000000000000000000000XX72731CC'.
[2024-12-20T06:17:35.995Z] Executing 'Functions.hello' (Reason='This function was programmatically called via the host APIs.', Id=dfdf3a03-e1b4-4790-bd26-52c6d1bbea42)
[2024-12-20T06:17:36.230Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.233Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/AppData/Roaming/npm/node_modules/azure-functions-core-tools/bin/workers/java/azure-functions-java-worker.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.250Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.252Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/azure-functions-java-library-3.0.0.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.256Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.258Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/jackson-core-2.14.1.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.263Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.267Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/spring-cloud-function-core-4.0.0.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.271Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.274Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/jackson-databind-2.14.1.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.279Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.282Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/spring-boot-starter-3.0.1.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.285Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.289Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/spring-messaging-6.0.3.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.293Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.297Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/spring-aop-6.0.3.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.301Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.305Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/jackson-annotations-2.14.1.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.309Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.312Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/javax.activation-api-1.2.0.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.317Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.321Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/spring-expression-6.0.3.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.324Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.327Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/reactor-core-3.5.1.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.331Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.334Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/lib/spring-cloud-function-context-4.0.0.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.342Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.344Z] INFO: Searching for start class in manifest: jar:file:/C:/Users/uname/hello-spring-function-azure/target/azure-functions/rkfn/hello-1.0-SNAPSHOT.jar!/META-INF/MANIFEST.MF
[2024-12-20T06:17:36.348Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
[2024-12-20T06:17:36.350Z] INFO: Loaded Start Class: class com.example.DemoApplication
[2024-12-20T06:17:36.354Z] Dec 20, 2024 11:47:36 AM org.springframework.cloud.function.utils.FunctionClassUtils getStartClass
//Removed few logs
[2024-12-20T06:17:37.127Z] Dec 20, 2024 11:47:37 AM org.springframework.boot.StartupInfoLogger logStarting
[2024-12-20T06:17:37.030Z] =========|_|==============|___/=/_/_/_/
[2024-12-20T06:17:37.034Z] :: Spring Boot :: (v3.0.1)
[2024-12-20T06:17:37.077Z] ======> SOURCE: class com.example.DemoApplication
[2024-12-20T06:17:37.127Z] Dec 20, 2024 11:47:37 AM org.springframework.boot.StartupInfoLogger logStarting
[2024-12-20T06:17:37.127Z] Dec 20, 2024 11:47:37 AM org.springframework.boot.StartupInfoLogger logStarting
[2024-12-20T06:17:37.132Z] INFO: Starting application using Java 21.0.4 with PID 10760 (started by user in C:\Users\uname\hello-spring-function-azure\target\azure-functions\rkfn)
[2024-12-20T06:17:37.136Z] Dec 20, 2024 11:47:37 AM org.springframework.boot.SpringApplication logStartupProfileInfo
[2024-12-20T06:17:37.140Z] INFO: No active profile set, falling back to 1 default profile: "default"
[2024-12-20T06:17:38.671Z] Dec 20, 2024 11:47:38 AM org.springframework.boot.StartupInfoLogger logStarted
[2024-12-20T06:17:38.675Z] INFO: Started application in 2.146 seconds (process running for 13.738)
[2024-12-20T06:17:38.692Z] Greeting user name: world
[2024-12-20T06:17:38.780Z] Function "hello" (Id: dfdf3a03-e1b4-4790-bd26-52c6d1bbea42) invoked by Java Worker
[2024-12-20T06:17:38.965Z] Executed 'Functions.hello' (Succeeded, Id=dfdf3a03-e1b4-4790-bd26-52c6d1bbea42, Duration=3004ms)