Kotlin+Spring Boot+GraphQL访问/graphql端点报405方法不允许错误
Spring Boot + GraphQL 405 METHOD_NOT_ALLOWED 错误排查线索
我正在构建一个Spring Boot + GraphQL的简单示例,代码如下:
package com.example.graphql import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.graphql.data.method.annotation.QueryMapping import org.springframework.web.bind.annotation.RestController @SpringBootApplication class GraphqlApplication fun main(args: Array<String>) { runApplication<GraphqlApplication>(*args) } @RestController class MyQueryController { @QueryMapping fun message(): Message = Message("1","some-text") } data class Message(var id: String, var text: String) { }
随后在src/main/resources/graphql/schema.graphqls路径下创建了如下schema:
type Query { message: Message } type Message { id: String text: String }
当尝试访问http://localhost:8080/graphql时,出现了[95221568-1] Completed 405 METHOD_NOT_ALLOWED, headers={masked}的错误,以下是排查线索:
- 请求方法不匹配:GraphQL端点默认仅支持POST请求,直接用浏览器访问会发送GET请求,必然返回405。需通过POST请求携带查询体访问,或启用可视化调试工具。
- 调整控制器注解:当前控制器使用
@RestController,虽能正常工作,但Spring GraphQL更推荐使用@Controller或@GraphQLController(Spring Boot 3.x+支持),避免不必要的Web MVC特性干扰。 - 验证代码与schema映射:确认schema中
Query的message字段与控制器@QueryMapping标注的方法名完全匹配(GraphQL大小写敏感),当前代码匹配无问题,但需排查拼写失误。 - 检查版本与配置冲突:使用的Spring Boot 3.0.0版本搭配
spring-boot-starter-graphql是兼容的,但需确认是否存在自定义WebMvc配置拦截了/graphql请求。 - 启用GraphiQL调试:在
application.properties中添加配置spring.graphql.graphiql.enabled=true,访问http://localhost:8080/graphiql即可通过可视化界面发送正确请求调试。 - 直接POST测试:用curl或Postman发送POST请求到http://localhost:8080/graphql,请求体如下:
{ "query": "{ message { id text } }" }
以下是项目的pom.xml依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> .... <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-graphql</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-kotlin</artifactId> </dependency> <dependency> <groupId>io.projectreactor.kotlin</groupId> <artifactId>reactor-kotlin-extensions</artifactId> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-reflect</artifactId> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId> </dependency> <dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-coroutines-reactor</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.graphql</groupId> <artifactId>spring-graphql-test</artifactId> <scope>test</scope> </dependency> </dependencies> ....
内容的提问来源于stack exchange,提问作者JPG
相关产品推荐
相关产品推荐

