使用Spring的RestTemplate发送GET请求,并支持传递body

1. 在pom.xml中添加依赖包

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

<version>4.3.4</version>

</dependency>

注意:版本需要在4.3.4以上,否则运行的时候会报java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients错误

2. 扩展HttpComponentsClientHttpRequestFactory

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);

}

/**

* 定义HttpGetRequestWithEntity实现HttpEntityEnclosingRequestBase抽象类,以支持GET请求携带body数据

*/

private static final class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase {
public HttpGetRequestWithEntity(final URI uri) {
super.setURI(uri);

}

@Override

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

}

}

}

3.在定义RestTemplate时,使用自定义factory

RestTemplate restTemplate = new RestTemplate();

//修改restTemplate的RequestFactory使其支持Get携带body参数

restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());

return restTemplate;

4.定义实体类

public class RequestBodyExample {
@JsonProperty(value = "provinceCode")

private String provinceCode;

@JsonProperty(value = "reference1")

private String reference1;

@JsonProperty(value = "requestId")

private String requestId;

public String getProvinceCode() {
return provinceCode;

}

public void setProvinceCode(String provinceCode) {
this.provinceCode = provinceCode;

}

public String getReference1() {
return reference1;

}

public void setReference1(String reference1) {
this.reference1 = reference1;

}

public String getRequestId() {
return requestId;

}

public void setRequestId(String requestId) {
this.requestId = requestId;

}

public RequestBodyExample() {
}

public RequestBodyExample(String provinceCode, String reference1, String requestId) {
this.provinceCode = provinceCode;

this.reference1 = reference1;

this.requestId = requestId;

}

}

5. 在controller中使用restTemplate

@RequestMapping(value = "/test",produces = {
"application/json;charset=UTF-8"}, method = RequestMethod.GET)

public String getTestdata(@RequestBody RequestBodyExample requestBodyExample) {
logger.info("测试获取get请求传递过来的body参数");

return JsonUtil.objToJson(requestBodyExample);

}

 

@RequestMapping(value = "/testGetbody", produces = {
"application/json;charset=UTF-8"}, method = RequestMethod.GET)

public String getbodyExample() {
logger.info("测试restTemplate get请求带body参数");

String url = "http://localhost:8081/test";

HttpHeaders httpHeaders = new HttpHeaders();

httpHeaders.setContentType(MediaType.APPLICATION_JSON);

httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

String body = "{\"provinceCode\": \"12\",\"reference1\": \"13\",\"requestId\": \"13\"}";

HttpEntity<String> requestEntity = new HttpEntity(body, httpHeaders);

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);

String result = response.getBody();

return result;

}

参考文章:http://www.belonk.com/c/http_resttemplate_get_with_body.html
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值