resttemplate调用

Map<String, Object> queryForHttp(String url, Map<String, Object> header, Map<String, Object> param, HttpMethod method) throws Exception{
// 默认使用Get方法查询
if (method == null)
method = HttpMethod.GET;
HttpHeaders headers = new HttpHeaders();
//设置默认为短连接
headers.add("Connection", "close");
if (!CollectionUtils.isEmpty(header)) {
for (String key : header.keySet()) {
headers.add(key, header.get(key).toString());
}
}
HttpEntity request = new HttpEntity<>(param, headers);

ResponseEntity<HashMap> exchange = restTemplate.exchange(url, method, request, HashMap.class);
HashMap result = null;
if (exchange != null && exchange.getBody() != null) {
result = exchange.getBody();
}
return result;
}
posted @ 2022-06-16 18:01  送一轮明月  阅读(157)  评论(0)    收藏  举报