请求异常RestTemplate 获取response的headers和body

        ResponseEntity<String> responseEntity = null;
        try {
            restTemplate.postForEntity("http://localhost:1111/test", null, String.class);
        } catch (HttpStatusCodeException e) {
            e.getStatusCode();
            e.getResponseBodyAsString();
            e.getResponseHeaders();
        }

捕捉HttpStatusCodeException替代RestClientException

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用`restTemplate`添加请求body时,可以使用以下几种方法: 1. 使用`postForObject`方法发送POST请求获取响应结果,将请求参数以`MultiValueMap`的形式存储在`paramMap`中,然后将`paramMap`作为参数传入`postForObject`方法中。例如: ``` MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>(); paramMap.add("dt", "20190225"); String result = template.postForObject(url, paramMap, String.class); ``` 2. 使用`postForEntity`方法发送POST请求获取响应结果。首先创建`HttpHeaders`和`HttpEntity`对象,将请求参数以`MultiValueMap`的形式存储在`paramMap`中,然后将`paramMap`和`headers`作为参数传入`HttpEntity`构造函数中,最后将`HttpEntity`对象作为参数传入`postForEntity`方法中。例如: ``` HttpHeaders headers = new HttpHeaders(); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers); ResponseEntity<String> response2 = template.postForEntity(url, httpEntity, String.class); ``` 3. 使用`exchange`方法发送POST请求获取响应结果。首先创建`HttpHeaders`和`HttpEntity`对象,将请求参数以`MultiValueMap`的形式存储在`paramMap`中,然后将`paramMap`和`headers`作为参数传入`HttpEntity`构造函数中,最后将`HttpEntity`对象作为参数传入`exchange`方法中。例如: ``` HttpHeaders headers = new HttpHeaders(); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers); ResponseEntity<String> response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class); ``` 以上是使用`restTemplate`添加请求body的几种方法,根据具体情况选择适合的方法进行使用。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [RestTemplate 添加请求headers和请求body](https://blog.csdn.net/u011138533/article/details/87920632)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [spring-boot-swagger.7z](https://download.csdn.net/download/bethzhang/11349186)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值