spring resttemplate get请求支持body

31 篇文章 1 订阅

pom中添加依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.9</version>
</dependency>

RestTemplate,默认用的 HttpComponentsClientHttpRequestFactory 工厂,其中HttpComponentsClientHttpRequestFactory 的 createHttpUriRequest 方法,发现 GET 请求是HttpGet,HttpGet 继承 HttpRequestBase,HttpRequestBase 是不支持传递body的。
 

HttpEntityEnclosingRequestBase是可以传递body的,那就自定义一个工厂继承HttpComponentsClientHttpRequestFactory,重写createHttpUriRequest方法,当发送的请求是GET请求时,创建自定义的继承HttpEntityEnclosingRequestBase的类,这样GET请求就可以传送body。

public class HttpComponentsClientRestfulHttpRequestFactory extends HttpComponentsClientHttpRequestFactory {
    @Override
    protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
        if (httpMethod == HttpMethod.GET) {
            return new HttpGetRequestWithEntity(uri);
        }
        return super.createHttpUriRequest(httpMethod, uri);
    }
}
public class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase {
    public HttpGetRequestWithEntity(final URI uri) {
        super.setURI(uri);
    }

    @Override
    public String getMethod() {
        return HttpMethod.GET.name();
    }
}

HttpEntity 的 httpheader 需要设置 headers.setContentType(MediaType.APPLICATION_JSON)

HttpComponentsClientRestfulHttpRequestFactory factory = new HttpComponentsClientRestfulHttpRequestFactory();
factory.setConnectTimeout(30000);
factory.setReadTimeout(60000);
this.restTemplate = new RestTemplate(factory);


HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> httpEntity = new HttpEntity<>(JSONObject.toJSONString(dataMap), headers);
ResponseEntity<byte[]> processEntity = restTemplate.exchange(processUri, HttpMethod.GET, httpEntity, byte[].class);

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值