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

使用API Gateway映射模板修改DynamoDB查询时,如何添加带冒号的属性值?

解决API Gateway映射模板中添加带冒号的DynamoDB属性值问题

你遇到的问题根源在于API Gateway映射模板使用的Velocity引擎,它不允许直接通过${vals.:myproperty}这种语法访问带冒号开头的键——Velocity的变量命名规则会把这种写法判定为无效。

要解决这个问题,你可以利用Velocity中Map对象的put方法,直接向ExpressionAttributeValues这个Map中添加带冒号的键值对,具体修改如下:

修正后的映射模板代码

## 处理FilterExpression的追加逻辑
#set($filter = $input.path('$').FilterExpression)
#if($filter == "") 
    #set($filter = "contains(myproperty, :myproperty)") 
#else 
    #set($filter = $filter + " AND contains(myproperty, :myproperty)") 
#end
#set($input.path('$').FilterExpression = $filter)

## 处理ExpressionAttributeValues的属性添加
#set($vals = $input.path('$').ExpressionAttributeValues)
#if($vals == "") 
    #set($vals = {}) 
#end

## 改用put方法添加带冒号的键值对
$vals.put(":myproperty", {'S': 'example'})
#set($input.path('$').ExpressionAttributeValues = $vals)

$input.json("$")

原理说明

  • Velocity中,$input.path('$').ExpressionAttributeValues返回的是一个Map类型对象,put方法是Map的标准方法,可以接受任意字符串作为键名,包括带冒号的特殊键。
  • 替换掉原来的#set(${vals.:myproperty} = ...)写法,改用$vals.put(":myproperty", ...)就能成功添加DynamoDB需要的:myproperty属性值。

这样修改后,你的请求就能被正确转换为目标格式,实现自动添加固定过滤表达式和对应属性值的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 20:35:12