Spring Boot 3.1.1中GraphQL请求出现405方法不允许问题求助
Spring Boot 3.1.1 GraphQL 405 Method Not Allowed 问题排查与解决
首先纠正你代码中的明显错误:
你的DemoGraphQLResolver方法存在类型不匹配问题——方法声明返回List<Party>,但实际返回的是字符串类型的phoneNumber参数,这会直接导致编译失败,先修正业务逻辑,示例如下:
@Controller public class DemoGraphQLResolver { @QueryMapping(name = "phoneNumber") public List<Party> phoneNumber(@Argument String phoneNumber) { // 替换为实际业务逻辑,比如构造Party对象返回 Party party = new Party(); party.setPhone(phoneNumber); return Collections.singletonList(party); } }
接下来按以下步骤排查405错误:
1. 验证Postman请求配置
- 确保请求方法为POST,URL准确对应
http://localhost:8802/graphql - 请求头必须设置
Content-Type: application/json,禁止使用form-data或x-www-form-urlencoded格式 - 请求体需符合GraphQL规范,示例如下(根据你的Schema调整字段):
{ "query": "{ phoneNumber(phoneNumber: \"13800138000\") { id phone } }" }
2. 确认Schema文件规范
检查src/main/resources/graphql下的.graphqls文件,确保Query类型和Party类型定义正确,示例:
type Query { phoneNumber(phoneNumber: String): [Party] } type Party { id: ID phone: String # 其他业务字段 }
3. 检查Spring Boot配置
- 确认
application.properties/application.yml未修改GraphQL端点路径,默认配置为:
spring.graphql.path=/graphql
- 若存在自定义WebMvc配置(如拦截器、过滤器),确保未拦截
/graphql路径的POST请求 - 查看启动日志,确认存在类似
Exposing POST /graphql的日志,说明端点已正确暴露
4. 排除冲突场景
- 检查是否有其他控制器(如
@RestController)映射了/graphql路径,导致请求路由冲突 - 若后续引入Spring Security依赖,需确保允许
/graphql路径的POST请求访问
内容的提问来源于stack exchange,提问作者Abhishek
相关产品推荐
相关产品推荐

