Spring Boot GraphQL自动配置启动时无法实例化GraphQlSource问题
我有一个基于Spring WebFlux和Spring GraphQL的Spring Boot应用,启动时自动配置机制无法实例化graphQlSource Bean,底层依赖的工厂方法执行失败,错误日志如下:
Factory method 'graphQlSource' threw exception with message: Error creating bean with name 'annotatedControllerConfigurerDataFetcherExceptionResolver' defined in class path resource [org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.class]: Unsatisfied dependency expressed through method 'annotatedControllerConfigurerDataFetcherExceptionResolver' parameter 0: Error creating bean with name 'annotatedControllerConfigurer' defined in class path resource [org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.class]: Error creating bean with name 'cursorStrategy' defined in class path resource [org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration$GraphQlDataAutoConfiguration.class]: Failed to instantiate [org.springframework.graphql.data.pagination.EncodingCursorStrategy]: Factory method 'cursorStrategy' threw exception with message: com/fasterxml/jackson/annotation/JsonTypeInfo$Value
我已检查Maven依赖树,但未发现引发问题的库版本冲突。当前使用的版本信息:
- Spring Boot 3.1.7
- Spring WebFlux 6.0.15
- Spring GraphQL 1.2.4(通过
spring-boot-starter-graphql 3.1.7引入)
- 显式锁定Jackson依赖版本
Spring Boot 3.1.x默认适配Jackson 2.15.x版本,可能存在隐式依赖导致版本不一致。在pom.xml中直接声明Jackson核心依赖,强制统一版本:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.15.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.15.3</version> </dependency>
- 清理并重新构建依赖
执行Maven命令清理本地缓存,重新拉取完整依赖:
mvn clean install -U
排查自定义Jackson配置
如果项目中有自定义Jackson配置类,确认未移除或修改JsonTypeInfo相关注解的支持,避免干扰Spring GraphQL内部的序列化逻辑。简化依赖引入方式
尝试直接引入Spring Boot官方starter,让框架自动管理所有依赖版本,减少手动指定版本引发的兼容性问题:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-graphql</artifactId> </dependency>
内容的提问来源于stack exchange,提问作者Fabien Coppens

