SpringBoot GraphQL控制器未找到致/graphql端点返回404错误
SpringBoot GraphQL端点404问题排查
问题描述
运行SpringBoot GraphQL示例项目时,GraphiQL浏览器可在localhost:8080/graphiql正常加载,但/graphql端点返回404错误。新增的WebFlux控制器可正常访问,但标注@QueryMapping的控制器无法工作。
错误信息
{ "timestamp": "2022-09-29T18:22:29.706+00:00", "status": 404, "error": "Not Found", "path": "/graphql" }
相关代码与配置
SampleController代码
@Controller public class SampleController { private final DataRepository repository; public SampleController(DataRepository dataRepository) { this.repository = dataRepository; } @QueryMapping public String greeting() { return this.repository.getBasic(); } @QueryMapping public Mono<String> greetingMono() { return this.repository.getGreeting(); } @QueryMapping public Flux<String> greetingsFlux() { return this.repository.getGreetings(); } @SubscriptionMapping public Flux<String> greetings() { return this.repository.getGreetingsStream(); } }
启动类代码
@SpringBootApplication public class SampleApplication { public static void main(String[] args) { SpringApplication.run(SampleApplication.class, args); } }
应用配置
spring.graphql.graphiql.enabled=true spring.graphql.graphiql.path=/graphiql spring.graphql.schema.printer.enabled=true spring.graphql.path=/graphql
POM依赖
<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> <dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-spring-boot-starter</artifactId> <version>5.0.2</version> </dependency> <dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-java-tools</artifactId> <version>5.2.4</version> </dependency> <dependency> <groupId>com.graphql-java</groupId> <artifactId>graphiql-spring-boot-starter</artifactId> <version>5.0.2</version> </dependency>
GraphQL Schema
type Query { greeting: String greetingMono : String greetingsFlux : [String] } type Subscription { greetings: String }
TestController代码
@RestController @RequestMapping("/test") public class TestController { public TestController() { } @GetMapping("/test2") Mono<String> test() { return Mono.just("hi"); } }
解决方案
1. 移除冲突依赖
你同时引入了Spring官方的spring-boot-starter-graphql和第三方com.graphql-java的GraphQL starter,二者存在版本冲突与功能重叠。保留spring-boot-starter-webflux和spring-boot-starter-graphql即可,其余三个com.graphql-java的依赖全部删除。Spring官方starter已内置GraphiQL支持,无需额外引入。
修改后的POM依赖:
<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>
2. 确认Schema文件位置
Spring GraphQL默认扫描src/main/resources/graphql/**/*.graphqls路径下的Schema文件,确保你的Schema文件放置在该目录下,或通过spring.graphql.schema.locations配置指定路径。
3. 验证请求方式
/graphql端点仅接受POST请求,直接用GET访问会返回404。需发送POST请求,请求体格式示例:
{ "query": "{ greeting }" }
4. 确保组件扫描范围
确认SampleController所在包在@SpringBootApplication的组件扫描范围内(即启动类所在包或其子包),若不在,需通过@ComponentScan指定扫描路径。
内容的提问来源于stack exchange,提问作者Paul C
相关产品推荐
相关产品推荐

