Spring Boot 3.4.0与Azure Functions Java兼容的GSON版本咨询
适配方案与对应Gson版本
1. 确定兼容的Gson版本
Spring Boot 3.4.0(对应Spring Framework 6.2.0)默认依赖的Gson版本为2.11.0,该版本不仅与Spring生态完全兼容,还包含setStrictness方法,能解决你遇到的方法不存在问题。
2. 解决Azure Functions Worker的类加载冲突
仅升级Gson版本无法解决根本问题——Azure Functions Java Worker会优先加载自带的旧版Gson,你需要通过打包配置强制应用使用你引入的版本:
- Maven项目:在
pom.xml中锁定Gson版本,并通过maven-shade-plugin重定位Gson包路径,避免类加载冲突:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.11.0</version> <scope>compile</scope> </dependency> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <relocations> <relocation> <pattern>com.google.gson</pattern> <shadedPattern>你的项目包名.shaded.com.google.gson</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> </plugins> </build>
- Gradle项目:使用
shadowJar插件实现包重定位:
dependencies { implementation 'com.google.code.gson:gson:2.11.0' } shadowJar { relocate 'com.google.gson', '你的项目包名.shaded.com.google.gson' }
3. 同步调整Spring Cloud Function版本
Spring Boot 3.4.0对应的Spring Cloud Function版本为4.3.0,建议将org.springframework.cloud:spring-cloud-function-dependencies从4.2.0同步升级到4.3.0,保证整体依赖生态的兼容性。
最终适配的完整依赖版本组合:
com.microsoft.azure.functions:azure-functions-java-library:3.1.0org.springframework.cloud:spring-cloud-function-dependencies:4.3.0com.google.code.gson:gson:2.11.0
内容的提问来源于stack exchange,提问作者Praveen
相关产品推荐
相关产品推荐

