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

已验证curl请求有效,如何用Spring RestTemplate.exchange发送整数列表?

使用Spring RestTemplate.exchange发送整数列表的方法

没问题,针对你要发送整数列表[23,24]的需求,用RestTemplate.exchange的核心操作很清晰,关键是把列表正确封装并匹配curl里的请求头配置:

  • 先准备好要发送的整数列表:

    List<Integer> ids = Arrays.asList(23, 24);
    
  • 构建和curl一致的请求头:

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setBearerAuth("XYZ"); // 替换为你的实际Bearer Token
    
  • 将列表和请求头封装成HttpEntity(这是RestTemplate发送请求的载体):

    HttpEntity<List<Integer>> requestEntity = new HttpEntity<>(ids, headers);
    
  • 最后调用exchange方法发送POST请求:

    restTemplate.exchange(
        "https://serverurl/api/list/GetByIds",
        HttpMethod.POST,
        requestEntity,
        YourResponseType.class // 替换为你实际期望的响应类型,比如具体的DTO类或ResponseEntity.class
    );
    

这样就完全对应你测试通过的curl命令了,请求体是JSON数组格式,请求头也和curl保持一致,能正确触发接口返回预期结果。

内容的提问来源于stack exchange,提问作者aquitted-mind

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:19:29