大哥们, springboot RestTemplate 的 exchange delete 可以传递 list 为参数吗

2021-03-25 12:00:29 +08:00
 liian2019

接口是这样的

    @DeleteMapping
    @ApiOperation("删除数据")
    public R<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return success(this.jobJdbcDatasourceService.removeByIds(idList));
    }

我的调用方式是这样的

@Override
    public void deleteDataSource(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return;
        }
        List<Long> datasourceIds = 			   ids.stream().map(Long::parseLong).collect(Collectors.toList());
        MultiValueMap<String,Object> params = new LinkedMultiValueMap<>();
        params.add("idList",datasourceIds);
        HttpEntity<MultiValueMap<String,Object>> entity = new HttpEntity<>(params,new HttpHeaders());
        ResponseEntity<DataXResponse> exchange = restTemplate.exchange(adminServer + dataSourcePath,HttpMethod.DELETE,entity,DataXResponse.class);
        boolean isSuccess = this.handleResponse(exchange,Boolean.class);
        if (!isSuccess){
            throw new FastdataException(FastdataPlatErrorCode.REST_CALL_FAILED.getCode(),"删除失败");
        }
    }

然后服务端报错了

Caused by: java.lang.NumberFormatException: For input string: "[12]"

意思就是需要一个 list,但是接受到一个 string

有大哥遇到过这样的情况吗

1468 次点击
所在节点    Java
8 条回复
ouyc
2021-03-25 12:29:16 +08:00
试试传一个用 , 隔开的字符串表示的 list
hantsy
2021-03-25 13:38:09 +08:00
标准的 Uri 这种参数传递应该都是支持三种格式的。

1. uri?a=1&a=2&a=3
2. uri?a=1,2,3
3. uri?a[]=1&a[]=2&a[]=3

如果你的生成 URI 不是的话,肯定会有问题。
hantsy
2021-03-25 13:39:33 +08:00
liian2019
2021-03-25 14:33:13 +08:00
@ouyc 还真是,这是什么原理大佬
liian2019
2021-03-25 14:33:29 +08:00
@hantsy 好的 谢谢你
liian2019
2021-03-25 14:35:40 +08:00
@hantsy 谢谢大佬
ouyc
2021-03-29 16:19:18 +08:00
@liian2019 这里是 http 协议 url 的参数部分,一般传的都是字符串,同时 spring mvc 定义了复杂参数传递的规则。复杂点的数据例如指定数组下标传参的和可以用
@hantsy 这种方式,传一整个数组就是 , 隔开的方式
liian2019
2021-03-30 18:29:55 +08:00
@ouyc 多谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/764953

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX