使用RestTemplate-如何将参数中的列表或数组从客户端传递到服务器

Abh*_*bhi 0 spring resttemplate

我有一个List<Integer>要发送到服务器的参数,Spring是否可以为此提供支持?

我尝试使用以下代码,

restTemplate.exchange("http://xxx.xx.xx.xx:8081/api/cart-items?cartItemsId={cartItemsId[]}", 
            HttpMethod.POST, httpEntity, Void.class, ArrayUtils.toPrimitive(cartItemsId.toArray(new Integer[cartItemsId.size()])));
Run Code Online (Sandbox Code Playgroud)

但是它向服务器发送如下内容:

http://xxx.xx.xx.xx:8081/api/cart-items?cartItemsId=%5BI@18275d8c

如果我使用,也会发生相同的问题:

restTemplate.exchange("http://xxx.xx.xx.xx:8081/api/cart-items?cartItemsId={cartItemsId[]}", 
            HttpMethod.POST, httpEntity, Void.class, cartItemsId);
Run Code Online (Sandbox Code Playgroud)

要么

restTemplate.exchange("http://xxx.xx.xx.xx:8081/api/cart-items?cartItemsId={cartItemsId[]}", 
            HttpMethod.POST, httpEntity, Void.class, cartItemsId.toArray(new Integer[cartItemsId.size()]));
Run Code Online (Sandbox Code Playgroud)

有什么方法可以使用参数将此列表或数组发送到服务器?

Sot*_*lis 5

String通过加入和的元素来构造a List<Integer>并将其作为uri变量提供。

String listOfIds = cartItemsId.stream().map(Object::toString).collect(Collectors.joining(","));
restTemplate.exchange("http://xxx.xx.xx.xx:8081/api/cart-items?cartItemsId={ids}",
    HttpMethod.POST, httpEntity, Void.class, listOfIds);
Run Code Online (Sandbox Code Playgroud)