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

AWS CDK:如何判断CfnMethod集成的具体类型(Mock/HTTP/Lambda等)

问题

我正在读取现有CloudFormation资源,需要获取CfnMethod的集成信息。CfnMethod.getIntegration()的返回类型为software.amazon.jsii.JsiiObject,我知道集成有Mock、HTTP、Lambda、AWSService等类型,且均继承自Integration类。尝试将JsiiObject转为Integration类时失败,报错“software.amazon.jsii.JsiiObject无法转为software.amazon.awscdk.services.apigateway.Integration”。尽管文档显示HttpIntegration等类的继承链包含Integration,但转换仍失败。想了解我的思路误区在哪里,以及如何判断集成的具体类型。

相关代码示例:

public static void execute(HolderForAWSState holderForAWSState){
        List<CfnMethod> work = new ArrayList<>();
        for (IConstruct worker : holderForAWSState.getOrderedConstructs().get(CfnMethod.class)) {
            work.add((CfnMethod) worker);
        }
        for(CfnMethod worker: work){
        System.out.println("Integration class: ");
        System.out.println(worker.getIntegration().getClass());
        Integration integration = (Integration) worker.getIntegration();
        System.out.println(integration.getClass());
        }
    }

文档中的继承链示例:

software.amazon.awscdk.services.apigateway
    Class HttpIntegration
        java.lang.Object
             software.amazon.jsii.JsiiObject
                 software.amazon.awscdk.services.apigateway.Integration
                     software.amazon.awscdk.services.apigateway.HttpIntegration
解答

思路误区分析

  • 混淆L1与L2构造的本质差异:CfnMethod是CloudFormation原生层级(L1)的构造,它的getIntegration()返回的是对应CFN资源的原始属性对象(本质是JsiiObject的动态代理实例);而Integration是CDK封装的L2抽象类,用于在构建阶段定义集成逻辑。L1构造仅负责映射CFN的原始资源定义,不会自动转换为CDK的L2抽象对象,因此强制类型转换必然失败。
  • 误解继承链的适用范围:文档中的继承链描述的是CDK L2集成类(如HttpIntegration)的层级关系,这些是CDK为简化开发封装的抽象,和L1构造返回的原始CFN属性不属于同一层面的对象。

判断集成具体类型的方法

要获取CfnMethod的集成类型,需直接解析其对应的CFN原始属性结构,而非试图转为L2的Integration类:

public static void execute(HolderForAWSState holderForAWSState){
        List<CfnMethod> work = new ArrayList<>();
        for (IConstruct worker : holderForAWSState.getOrderedConstructs().get(CfnMethod.class)) {
            work.add((CfnMethod) worker);
        }
        for(CfnMethod worker: work){
            Object integrationObj = worker.getIntegration();
            // 将JsiiObject转换为Map,直接访问CFN集成属性
            Map<String, Object> integrationProps = (Map<String, Object>) software.amazon.jsii.JsiiObject.asMap(integrationObj);
            String integrationType = (String) integrationProps.get("type");
            
            System.out.println("当前集成类型:" + integrationType);
            
            // 根据类型解析对应专属属性
            switch(integrationType){
                case "HTTP":
                case "HTTP_PROXY":
                    String httpUri = (String) integrationProps.get("uri");
                    System.out.println("HTTP集成目标地址:" + httpUri);
                    break;
                case "AWS":
                case "AWS_PROXY":
                    String awsUri = (String) integrationProps.get("uri");
                    System.out.println("AWS服务集成ARN:" + awsUri);
                    break;
                case "MOCK":
                    System.out.println("当前为Mock集成");
                    break;
                default:
                    System.out.println("未识别的集成类型:" + integrationType);
            }
        }
    }

核心逻辑说明

CloudFormation的AWS::ApiGateway::Method资源中,Integration属性本身是一个JSON结构,其中Type字段直接标识了集成类型(对应代码中的integrationType)。不同类型的集成会附带对应的专属属性,比如HTTP类型的Uri、AWS服务类型的Uri(格式通常为arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda-arn>/invocations),直接解析这些属性即可获取所需信息。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:35:24