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

导入OpenAPI YAML至API Gateway遇400错误:已定义响应仍报错

解决AWS API Gateway导入OpenAPI YAML时的"No integration defined for method"错误

你搞混了错误的原因——这个报错不是因为你没定义400响应,而是你OpenAPI YAML里的/和/weather这两个GET方法没有配置后端集成。AWS API Gateway要求每个接口方法必须关联后端服务(比如Lambda、HTTP接口等),否则无法完成部署。

看你的CDK代码,你只给新增的/books路径加了Lambda集成,但完全没管YAML里自带的两个路径方法,这才是触发错误的核心问题。

两种解决思路

思路1:保留YAML里的接口并添加集成

如果/和/weather是你需要的接口,要么在YAML里直接通过AWS扩展字段配置集成,要么在CDK里给这两个方法补加集成:

const integration = new LambdaIntegration(hello)

const api = new SpecRestApi(this, 'books-api', {
  apiDefinition: ApiDefinition.fromAsset('apiTrial.yaml')
});

// 给YAML里的根路径GET方法加集成
const rootGetMethod = api.root.getMethod('GET');
rootGetMethod.addIntegration(integration);

// 给YAML里的/weather路径GET方法加集成
const weatherResource = api.root.getResource('weather');
const weatherGetMethod = weatherResource.getMethod('GET');
weatherGetMethod.addIntegration(integration);

// 新增/books路径的逻辑保留
const booksResource = api.root.addResource('books')
booksResource.addMethod('GET', integration);

思路2:删除YAML里不需要的接口

如果/和/weather是测试用的不需要保留,直接删掉YAML里的paths部分,只保留基础结构,再用CDK定义需要的接口即可:
修改后的YAML示例:

openapi: 3.0.0
info:
  title: "MyFirstAPI"
  version: "1.0.0"
  contact:
    name: Karim Fayed
    email: contact@example.com
    url: https://example.com/
tags:
  - name: Metadata
    description: Metadata related requests
servers:
- url: "https://api.openweathermap.org/data/2.5"
paths: {} # 留空或者直接删掉paths节点

另外注意:错误提示里的Status Code: 400是API Gateway返回的请求错误码,和你接口定义的400响应完全是两回事,别搞混了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 12:15:41