开发者社区 问答 正文

RestTemplate Post请求,参数传不过去 400 请求报错 

我写了几个增加和查询接口,调用第三方提供的接口时,用了restTemplate的postForEntity请求,发现参数传不过去,响应为null, 然后显示连接超时。

展开
收起
kun坤 2020-05-29 16:28:27 2844 分享 版权
1 条回答
写回答
取消 提交回答
  • 开局一段儿话,异常全靠猜。没JB你说个代码。######

    HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"); //封装参数,千万不要替换为Map与HashMap,否则参数无法传递 MultiValueMap<String, String> params= new LinkedMultiValueMap<>(); //添加请求的参数 params.add("hello", "hello");             //必传 params.add("world", "world");           //选传 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);

    2020-05-29 16:28:33
    赞同 展开评论
问答地址: