部署为AWS Lambda的Spring Boot应用GET请求多值Query参数丢失
问题描述
向部署为AWS Lambda函数的Spring Boot应用发送以下GET请求:
GET https://my-dev-host.com/fdx/v6/accounts?accountIds=confUser_1&accountIds=confUser_2&accountIds=confUser_3&accountIds=confUser_4&offset=1&limit=3 Authorization: Bearer .... Accept: application/json Accept-Charset: UTF-8 Content-Type: application/json
但日志显示仅最后一个accountId到达控制器:
Modified request: { "multiValueQueryStringParameters": { "accountIds": [ "confUser_4" ], "limit": [ "3" ], "offset": [ "1" ] }, "queryStringParameters": { "accountIds": "confUser_4", "limit": "3", "offset": "1" }, "multiValueHeaders": { "accept": [ "application/json" ], "accept-charset": [ "UTF-8" ], "accept-encoding": [ "br, deflate, gzip, x-gzip" ], "authorization": [ "Bearer ..." ], "content-type": [ "application/json" ], "host": [ "my-dev-host.com" ] }, "headers": { "accept": "application/json", "accept-charset": "UTF-8", "accept-encoding": "br, deflate, gzip, x-gzip", "authorization": "Bearer ...", "content-type": "application/json", "host": "my-dev-host.com" }, "pathParameters": {}, "httpMethod": "GET", "stageVariables": {}, "path": "/fdx/v6/accounts", "isBase64Encoded": false, "requestSource": "ALB" }
尝试改用逗号分隔的传参方式,问题依旧:
GET https://my-dev-host.com/fdx/v6/accounts?accountIds=confUser_1,confUser_2,confUser_3,confUser_4&offset=1&limit=3 Authorization: Bearer .... Accept: application/json Accept-Charset: UTF-8 Content-Type: application/json
控制器API定义如下:
@RequestMapping( method = RequestMethod.GET, value = "/accounts", produces = {"application/json"} ) ResponseEntity<Object> searchForAccounts( @Parameter(name = "accountIds", description = "Comma separated list of account ids", in = ParameterIn.QUERY) @Valid @RequestParam(value = "accountIds", required = false) List<String> accountIds, @Parameter(name = "offset", description = "Opaque cursor used by the provider to send the next set of records", in = ParameterIn.QUERY) @Valid @RequestParam(value = "offset", required = false) String offset, @Parameter(name = "limit", description = "Number of elements that the consumer wishes to receive. Providers should implement reasonable default and maximum values", in = ParameterIn.QUERY) @Valid @RequestParam(value = "limit", required = false) Integer limit );
解决方案
1. 开启ALB目标组多值查询参数支持
请求来源为ALB,默认情况下ALB会将重复的查询参数覆盖为最后一个值,需调整配置:
- 进入AWS控制台EC2服务,找到对应ALB的目标组
- 编辑目标组属性,将「多值查询字符串」设置为「启用」
- 保存配置后重新测试请求
2. 适配逗号分隔参数的解析逻辑
如果需要支持逗号分隔的传参方式,需在@RequestParam中指定分隔符:
@RequestParam(value = "accountIds", required = false, delimiter = ",") List<String> accountIds
配置后Spring会自动将逗号分隔的字符串拆分为List集合
3. 验证中间转发层的参数处理
如果ALB与Lambda之间存在API Gateway等转发层,需同步开启对应组件的多值查询参数支持,确保完整的参数列表能传递到Lambda函数中
内容的提问来源于stack exchange,提问作者Sergey Tsypanov
相关产品推荐
相关产品推荐

