javaspring-bootazure-functionsgson

Spring boot, azure-functions-java-worker and GSON lib compatibility Issue


I have upgraded my java azure function dependencies from Spring boot 3.3.0 -> spring boot 3.4.0 and using the below dependencies.

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

Now, I start seeing issues. I tried upgrading the gson version 2.11.0/2.10.1 and even removed it but I still getting the below error.

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)'

adding more error details below:

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.

What is the right version of GSON to use along with spring boot 3.4.0 , spring framework 6.2.0 and azure functions java library 3.1.0?

Thanks in advance for the help :).


Solution

  • 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)'
    

    The issue could be due to the conflict between dependencies versions you have used.

    Thanks for the insights @E.Makarovas.

    Spring Boot has the correct gson jar configured as a dependency which will be included in your build automatically. When you specify a dependency to gson explicitly, it overrides the default dependency version brought by Spring Boot.

    To resolve this error:

    1. Remove the explicit dependency to gson from your pom.xml (or) build.gradle.
    2. Or update the gson dependency com.google.code.gson:gson to 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
    
    1. Remove the dependency which has Gson class or exclude "AutoConfiguration" for Gson class by adding below line in the Main class of Spring Boot Application:
    @EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
    

    Code Snippet:

    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);
        }
    }
    

    I have created a Spring Boot Azure function with latest versions of the libraries.

    <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>
    

    You can check the installed dependency versions in the console output below:

    Console Output:

    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)