介绍
- Java-RestTemplate是Spring框架中的一个类,用于发送HTTP请求并与RESTful服务进行交互。它提供了一种简单且方便的方式来调用远程的RESTful服务,并对响应进行处理。
- RestTemplate提供了多种方法来发送不同类型的请求(GET,POST,PUT,DELETE等),并支持将请求参数和请求体与URL进行绑定。它还可以处理不同的响应类型(JSON,XML等),并将响应转换为Java对象。
常用方法
- getForObject(String url, Class<T> responseType, Object... uriVariables): 发送GET请求并将服务器响应映射为指定类型的对象。uriVariables参数用于替换URL模板中的占位符。
- getForEntity(String url, Class<T> responseType, Object... uriVariables): 发送GET请求并将服务器响应映射为ResponseEntity对象。ResponseEntity将包含响应的状态码、头部信息和响应体。
- postForObject(String url, Object request, Class<T> responseType, Object... uriVariables): 发送POST请求并将请求体映射为指定类型的对象。request参数是请求的实体对象。
- postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables): 发送POST请求并将请求体映射为ResponseEntity对象。ResponseEntity将包含响应的状态码、头部信息和响应体。
- put(String url, Object request, Object... uriVariables): 发送PUT请求,并将请求体作为参数发送到指定的URL。
- delete(String url, Object... uriVariables): 发送DELETE请求到指定的URL。
- exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables): 发送HTTP请求,并将请求体映射为指定类型的对象。requestEntity参数是包含请求头和请求体的HttpEntity对象。
- execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Object... uriVariables): 发送HTTP请求,并在回调中处理请求和响应。requestCallback参数用于配置请求,responseExtractor参数用于处理响应。
工具封装
package com.seekgenjs.position.util;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* Http请求工具
*/
public class HttpClientUtil {
Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
/**
* 设置请求头
* @return
*/
private static HttpHeaders getHttpHeaders(HttpMethod method,MultiValueMap<String,String> values) {
HttpHeaders headers = new HttpHeaders();
if (method.equals(HttpMethod.GET)){
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
}
if (method.equals(HttpMethod.POST)){
headers.setContentType(MediaType.APPLICATION_JSON);
}
if (values != null){
headers.addAll(values);
}
return headers;
}
/**
* get请求
* @param URL url
* @param body body
* @return json
*/
public JSONObject httpGet(String URL,String body,MultiValueMap<String,String> header){
RestTemplate template = new RestTemplate();
logger.debug("{}_get请求",URL);
HttpHeaders headers = getHttpHeaders(HttpMethod.GET, header);
HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> response = template.exchange(URL, HttpMethod.GET,requestEntity, String.class);
return JSONObject.parseObject(response.getBody());
}
/**
* post请求
* @param URL url
* @param body body
* @return json
*/
public JSONObject httpPost(String URL, HashMap<String, Object> body,MultiValueMap<String,String> header){
RestTemplate template = new RestTemplate();
logger.debug("{}_post请求",URL);
HttpHeaders headers = getHttpHeaders(HttpMethod.POST, header);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> response = template.postForEntity(URL,requestEntity, String.class);
return JSONObject.parseObject(response.getBody());
}
}
使用案列
private final HttpClientUtil httpClient = new HttpClientUtil();
JSONObject jsonObject = httpClient.httpPost(url, body, header);