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

如何通过Spring RestTemplate在GET请求中传递Java List至Controller

解决RestTemplate传递List到Spring Controller的问题

首先,你当前代码的问题在于直接把List<String>拼接到URL中,这样生成的参数会是ids=[id1,id2,id3]这种格式,服务端无法正确解析为多个独立的id参数。下面分GET和POST两种请求场景给出正确实现:

场景1:GET请求传递List参数

客户端代码(RestTemplate)

推荐使用UriComponentsBuilder来构建URL,它会自动帮你把List转换成ids=id1&ids=id2的正确格式:

public String generateInfoOfIds(List<String> ids, String userName, String password) {
    RestTemplate restTemplate = new RestTemplate();
    // 添加Basic认证拦截器
    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(userName, password));
    
    // 用UriComponentsBuilder构建带List参数的URL
    UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl("https://someservice.com/getInfoOfIds")
            .queryParam("ids", ids) // 直接传入List,自动拆分多个ids参数
            .build();
    
    // 发送GET请求
    ResponseEntity<String> response = restTemplate.exchange(
            uriComponents.toUriString(),
            HttpMethod.GET,
            null, // GET请求无请求体,传null
            String.class
    );
    return response.getBody();
}

服务端Controller代码

用@RequestParam注解接收List,注意参数名要和客户端传入的一致:

@RestController
@RequestMapping("/")
public class InfoController {
    @GetMapping("/getInfoOfIds")
    public ResponseEntity<String> getInfoOfIds(
            @RequestParam("ids") List<String> ids, // 直接接收List
            HttpServletRequest request
    ) {
        // 处理你的业务逻辑
        return ResponseEntity.ok("处理完成,接收的ids:" + ids);
    }
}

场景2:POST请求传递List参数

如果更适合用POST传递(比如ids数量很多,URL长度受限),可以把List放到请求体或者form参数中:

方式A:请求体传递JSON格式的List

客户端代码

public String generateInfoOfIds(List<String> ids, String userName, String password) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(userName, password));
    
    // 设置请求头为JSON格式
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    
    // 把List作为请求体
    HttpEntity<List<String>> requestEntity = new HttpEntity<>(ids, headers);
    
    ResponseEntity<String> response = restTemplate.exchange(
            "https://someservice.com/getInfoOfIds",
            HttpMethod.POST,
            requestEntity,
            String.class
    );
    return response.getBody();
}

服务端Controller代码

用@RequestBody接收JSON数组:

@PostMapping("/getInfoOfIds")
public ResponseEntity<String> getInfoOfIds(@RequestBody List<String> ids) {
    // 处理业务逻辑
    return ResponseEntity.ok("接收的ids:" + ids);
}

方式B:POST表单传递List参数

客户端代码

public String generateInfoOfIds(List<String> ids, String userName, String password) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(userName, password));
    
    // 构建表单参数,把List拆分成多个同名称的参数
    MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
    for (String id : ids) {
        formData.add("ids", id);
    }
    
    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, null);
    
    ResponseEntity<String> response = restTemplate.exchange(
            "https://someservice.com/getInfoOfIds",
            HttpMethod.POST,
            requestEntity,
            String.class
    );
    return response.getBody();
}

服务端Controller代码

同样用@RequestParam接收List:

@PostMapping("/getInfoOfIds")
public ResponseEntity<String> getInfoOfIds(@RequestParam("ids") List<String> ids) {
    // 处理业务逻辑
    return ResponseEntity.ok("接收的ids:" + ids);
}

关键注意点

  • 不要手动拼接List到URL,会导致格式错误,优先用UriComponentsBuilder处理GET的query参数
  • GET请求的List参数默认是通过多个同名称的query参数传递(ids=1&ids=2),Spring会自动把它们组装成List
  • POST请求如果用JSON传递,要确保请求头设置为application/json,服务端用@RequestBody接收
  • 如果你的服务端需要指定参数的分隔符(比如用ids=1,2,3这种逗号分隔的形式),那客户端需要把List转成逗号分隔的字符串,服务端再拆分,但这种方式不如上面的标准方式通用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:10:06