【问题标题】:How to access AWS API Gateway Models from iOS如何从 iOS 访问 AWS API Gateway 模型
【发布时间】:2016-06-14 05:11:03
【问题描述】:

AWS 模型架构

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "QuestionsModel",
    "type": "array",
    "items": {
    "type": "object",
    "properties": {
        "placeholder" : { "type":"string" },
        "type" : { "type":"string" },
        "order": { "type": "integer" },
        "prompt": { "type": "string" },
        "section_name": { "type": "string" }
        }
    }
}

AWS 集成响应 - 映射模板 - application/json

使用 Velocity 模板语言进行映射 一个数组...

#set($inputRoot = $input.path('$'))
[
#foreach($elem in $inputRoot)
{
  "type" : "$elem.type",
  "placeholder" : "$elem.placeholder",
  "order" : "$elem.order",
  "prompt" : "$elem.prompt",
  "section_name" : "$elem.section_name"
} 
#if($foreach.hasNext),#end
#end
]

AWS Lambda 函数

def lambda_handler(event, context):
    client = boto3.client('dynamodb')

    response = client.scan(
        TableName='Question',
        AttributesToGet=[
            'type',
            'order',
            'section_name',
            'prompt',
            'placeholder'])

    return = response['Items']

iOS 应用模型

iOS 模型有一个 NSString 类型的字段 type 填充了值 {S=Hello World}

我宁愿 iOS 字段等于 Hello World 省我解析 {S=*}

我哪里错了?

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway vtl


    【解决方案1】:

    你在方法response中设置了响应模型吗? 这是 API Gateway 提供的基本演练。 http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-models.html

    【讨论】:

    • 是的。我已经关注了该页面并构建了一个 iOS API。我注意到我定义的模型。我会调查我是否正确地从 lambda/dynamodb 返回数组。
    【解决方案2】:

    我在another question 中确定了答案。

    未记录,但您可以在映射模板中的字段名称后简单地指定类型:

    #set($inputRoot = $input.path('$'))
    [
    #foreach($elem in $inputRoot)
    {
      "field1" : "$elem.field1.S",
      "field2" : $elem.field2.N
    }#if($foreach.hasNext),#end
    #end
    ]
    

    请注意,字符串字段需要用引号分隔,数字不需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 2020-11-12
      • 2020-02-20
      • 1970-01-01
      • 2016-02-06
      相关资源
      最近更新 更多