使用Spring的RestTemplate发送GET请求,并支持传递Request body参数详细实例

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

org.apache.httpcomponents

httpclient

4.3.4

注意:版本需要在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 requestEntity = new HttpEntity(body, httpHeaders);

ResponseEntity 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

你可能感兴趣的:(spring,boot)