You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地运行Spring Cloud Function遇Gson类缺失错误,需排除自动配置?

问题:Spring Cloud Function + Azure 本地运行出现 ClassNotFoundException: com.google.gson.Strictness,排除GsonAutoConfiguration是否合理?

我基于以下配置开发Spring Cloud Function示例,本地运行访问URL时触发java.lang.ClassNotFoundException: com.google.gson.Strictness错误。目前通过排除GsonAutoConfiguration类可以缓解问题,想确认这是否属于预期情况?


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.azlearning</groupId>
    <artifactId>az-email-sender-function</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>az-email-sender-function</name>
    <description>Azure Email Sender function</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>

    <properties>
        <java.version>17</java.version>
        <spring-boot-thin-layout.version>1.0.31.RELEASE</spring-boot-thin-layout.version>

        <!-- Spring Boot start class! WARING: correct class must be set! -->
        <start-class>com.azlearning.functionapp.AzEmailSenderFunctionApplication</start-class>

        <!-- AZURE FUNCTION CONFIG -->
        <azure.functions.maven.plugin.version>1.36.0</azure.functions.maven.plugin.version>
        <functionAppName>email-sender-funapp</functionAppName>
        <functionAppRegion>Region</functionAppRegion>
        <functionResourceGroup>az-cert-resource-group</functionResourceGroup>
        <functionAppServicePlanName>PlanName</functionAppServicePlanName>
        <functionPricingTier>PricingTier</functionPricingTier>
        <spring-cloud.version>2024.0.0</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-context</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-azure</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-functions-maven-plugin</artifactId>
                <version>${azure.functions.maven.plugin.version}</version>

                <configuration>
                    <appName>${functionAppName}</appName>
                    <resourceGroup>${functionResourceGroup}</resourceGroup>
                    <region>${functionAppRegion}</region>
                    <appServicePlanName>${functionAppServicePlanName}</appServicePlanName>
                    <pricingTier>${functionPricingTier}</pricingTier>

                    <hostJson>${project.basedir}/src/main/resources/host.json</hostJson>
                <!--    <localSettingsJson>${project.basedir}/src/main/resources/local.settings.json</localSettingsJson> -->

                    <runtime>
                        <os>linux</os>
                        <javaVersion>17</javaVersion>
                    </runtime>

                    <funcPort>7072</funcPort>

                    <appSettings>
                        <property>
                            <name>FUNCTIONS_EXTENSION_VERSION</name>
                            <value>~4</value>
                        </property>
                    <!--    <property>
                            <name>MAIN_CLASS</name>
                            <value>com.azlearning.functionapp.AzEmailSenderFunctionApplication</value>
                        </property> -->
                    </appSettings>
                </configuration>
                <executions>
                    <execution>
                        <id>package-functions</id>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--        <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <mainClass>com.azlearning.functionapp.AzEmailSenderFunctionApplication</mainClass>
                            <excludes>
                                <exclude>
                                    <groupId>org.projectlombok</groupId>
                                    <artifactId>lombok</artifactId>
                                </exclude>
                            </excludes>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>org.springframework.boot.experimental</groupId>
                                <artifactId>spring-boot-thin-layout</artifactId>
                                <version>1.0.31.RELEASE</version>
                            </dependency>
                        </dependencies>
            </plugin> -->
        </plugins>
    </build>

</project>

启动类 AzEmailSenderFunctionApplication.java

//@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
@SpringBootApplication
public class AzEmailSenderFunctionApplication {

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

}

函数处理器 EmailSenderFunctionHandler.java

@Component
public class EmailSenderFunctionHandler {

    private final EmailSender emailSender;

    public EmailSenderFunctionHandler(EmailSender emailSender) {
        this.emailSender = emailSender;
    }

    @FunctionName("emailSender")
    public String execute(
            @HttpTrigger(name = "req", methods = {
                    HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<EmailDTO>> request,
            ExecutionContext context) {

        context.getLogger().info("Java HTTP trigger processed a request.");

        context.getLogger().info("Get Body value : " + request.getHeaders());
        context.getLogger().info("Get Body value : " + request.getUri());

        context.getLogger().info("Get Body value : " + request.getBody());

        if(!request.getBody().isPresent()) {
            return "Please pass a emailDTO in the request body";
        }

        EmailDTO emailDTO = request.getBody().get();

        context.getLogger().info("From : " + emailDTO.getFrom());
        context.getLogger().info("To : " + emailDTO.getTo());
        context.getLogger().info("Subject : " + emailDTO.getSubject());
        context.getLogger().info("Body : " + emailDTO.getBody());
        return emailSender.apply(emailDTO);

    }
}

解答

这个问题不属于预期情况,本质是依赖版本冲突导致的:

  1. 问题根源

    • Spring Boot 3.4.0 默认依赖的 Gson 版本移除了com.google.gson.Strictness类(该类在 Gson 2.10+ 版本中被标记为过时,后续版本直接删除)。
    • 你引入的spring-cloud-function-adapter-azure或其依赖组件,可能依赖了旧版 Gson,或者在自动配置时尝试加载已被移除的类,触发ClassNotFoundException。
  2. 排除GsonAutoConfiguration的合理性

    • 如果你不需要 Gson 作为 JSON 序列化工具(Spring Boot 默认使用 Jackson),排除该自动配置类是可行的临时解决方案,能避免触发错误的自动配置逻辑。
    • 但这不是最优解,更推荐的做法:
      • 显式指定兼容的 Gson 版本,在 pom.xml 的properties中添加<gson.version>2.11.0</gson.version>或更高兼容版本。
      • 执行mvn dependency:tree检查依赖树,找出引入旧版 Gson 的组件,通过<exclusions>排除冲突依赖。
  3. 额外注意点

    • 你的项目未显式引入 Gson 依赖,Spring Boot 自动配置会按需加载,但 Azure Function 适配器可能隐含依赖 Gson,导致版本不匹配。
    • 若后续需要使用 Gson,排除自动配置类会导致相关功能失效,此时必须解决版本冲突问题。

内容的提问来源于stack exchange,提问作者springbootlearner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:59:56