在项目中,我们可能需要发起HTTP请求,请求目标URL来获取响应数据做一些处理。就需要用到HTTP请求工具,常用的Java类工具有:HttpUrlConnection、Apache HttpClient、OK Http等,这些可能需要一些繁琐的配置,不太适合Spring项目的开发。Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的工具。
这里也推荐另一款HTTP请求工具:https://square.github.io/retrofit/
详细介绍可参考官网:RestTemplate
常用的xxxForObject和xxxForEntity,是对具体请求方法的简单封装。其中,exchange和execute是功能更丰富的请求方式。
引入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
示例请求接口
package com.hzl.controller;
import com.alibaba.fastjson.JSON;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List;
/**
* @Author: zhilin
* @Date: 2023/7/3 下午11:12
* @Description: TODO
*/
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/getOne")
public Object getOne() {
return LocalDate.now().toString();
}
@GetMapping("/getTwo/{name}")
public Object getTwo(@PathVariable String name, @RequestParam String type) {
System.out.println(name + " = " + type);
return String.format("%s,%s", name, LocalDate.now().toString());
}
@GetMapping("/getThree/{name}")
public Object getThree(@PathVariable String name, @RequestParam Boolean isTo, @RequestParam String type, @RequestParam List<Integer> list) {
System.out.println(name + " " + " " + isTo + " " + type + " " + list);
return String.format("%s,%s,%s", name, type, LocalDate.now().toString());
}
@GetMapping("/getFour/{name}")
public Object getFour(@PathVariable String name, @RequestParam Boolean isTo, @RequestHeader MultiValueMap map, @RequestParam String type, @RequestParam List<Integer> list) {
System.out.println(name + " " + isTo + " " + map + " " + type + " " + list);
int i = 1 / 0;
return String.format("%s,%s,%s", name, type, LocalDate.now().toString());
}
@PostMapping("/postOne/{name}")
public Object postOne(@PathVariable String name, @RequestParam String type, @RequestParam String color) {
return String.format("%s==%s==%s", name, type, color);
}
@PostMapping("/postTwo/{name}")
public Object postTwo(@PathVariable String name, @RequestParam Integer type, @RequestBody LinkedMultiValueMap map) {
System.out.println("name = " + name);
System.out.println("type = " + type);
System.out.println("map = " + JSON.toJSONString(map));
return String.format("%s==%s==%s", name, type, map);
}
@PostMapping("/postThree/{name}")
public Object postThree(@PathVariable String name, @RequestParam Integer type, @RequestBody String json) {
System.out.println("name = " + name);
System.out.println("type = " + type);
System.out.println("json = " + json);
return String.format("%s==%s==%s", name, type, json);
}
}
Http请求参数封装
package com.hzl.controller;
import lombok.Data;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: zhilin
* @Date: 2023/7/8 下午4:13
* @Description: 请求参数封装
*/
@Data
public class HttpRequest {
/**
* 请求的地址如:http://localhost:8090/test/postTwo/{name}
*/
private String url;
/**
* 请求的方法(GET/POST)
*/
private String method;
/**
* 请求的格式(form/json)
*/
private String contentType;
/**
* 请求头部参数
*/
private Map<String, List<String>> headerMap;
/**
* 请求path参数
*/
private Map<String, String> pathMap = new LinkedHashMap<>();
/**
* 请求的query参数,url中?后面的部分
*/
private Map<String, List<String>> queryMap;
/**
* 使用 application/x-www-form-urlencoded时请求的body参数集合
*/
private Map<String, List<String>> bodyMap;
/**
* 使用 application/json时请求body的参数
*/
private String body;
}
封装测试工具类
package com.hzl.controller;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @Author: zhilin
* @Date: 2023/7/8 下午4:24
* @Description: TODO
*/
public class HttpRequestUtil {
// private HttpRequestUtil() {
// }
//
// public static class HttpRequestHolder {
// public static final HttpRequestUtil INSTANCE = new HttpRequestUtil();
// }
//
// public static HttpRequestUtil getInstance() {
// return HttpRequestHolder.INSTANCE;
// }
private static final RestTemplate RESTTEMPLATE = new RestTemplate();
static {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(10 * 1000);
requestFactory.setReadTimeout(30 * 1000);
RESTTEMPLATE.setRequestFactory(requestFactory);
RESTTEMPLATE.setErrorHandler(new AcceptResponseErrorHandler());
}
/**
* 使RestTemplate能够对响应的错误消息不进行处理
* 如:当响应码为400、500等错误时,能够不进行处理,最终用户可以获取到body数据
*/
private static class AcceptResponseErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse response) {
// 返回false表示不管response的status是多少都返回没有错误
return false;
}
@Override
public void handleError(ClientHttpResponse response) {
// 实现你自己业务上遇到了Error进行处理逻辑
System.out.println("response = " + response);
}
}
public static void exchange(HttpRequest request) {
}
public static ResponseEntity get(HttpRequest request) {
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(request.getUrl())
.queryParams(convertOfString(request.getQueryMap()))
.buildAndExpand(request.getPathMap())
.encode();
ResponseEntity<String> entity = RESTTEMPLATE.getForEntity(uriComponents.toUri(), String.class);
return entity;
}
public static ResponseEntity getOfHeader(HttpRequest request) {
// 请求头
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.parseMediaType(request.getContentType()));
// 比如如下为系统参数
httpHeaders.add("id", "666");
httpHeaders.add("pay", "all");
httpHeaders.addAll(convertOfString(request.getHeaderMap()));
// 请求体
HttpEntity httpEntity = new HttpEntity(httpHeaders);
// uri
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(request.getUrl());
// 填充query参数
builder.queryParams(convertOfString(request.getQueryMap()));
//填充path参数
UriComponents uriComponents = builder.buildAndExpand(request.getPathMap());
ResponseEntity<String> entity = RESTTEMPLATE.exchange(uriComponents.toUri(), HttpMethod.GET, httpEntity, String.class);
return entity;
}
public static ResponseEntity post(HttpRequest request) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.parseMediaType(request.getContentType()));
// httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 比如如下为系统参数
httpHeaders.add("id", "666");
httpHeaders.add("pay", "all");
httpHeaders.addAll(convertOfString(request.getHeaderMap()));
// 请求方式为application/x-www-form-urlencoded,取bodyMap
HttpEntity httpEntity;
if (MediaType.APPLICATION_FORM_URLENCODED_VALUE.equals(request.getContentType())) {
httpEntity = new HttpEntity(convertOfString(request.getBodyMap()), httpHeaders);
} else {
httpEntity = new HttpEntity(request.getBody(), httpHeaders);
}
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(request.getUrl());
builder.queryParams(convertOfString(request.getQueryMap()));
UriComponents uriComponents = builder.buildAndExpand(request.getPathMap());
ResponseEntity<String> entity = RESTTEMPLATE.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, String.class);
return entity;
}
public static MultiValueMap convertOfObject(Map<String, Object> map) {
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
if (map != null && !map.isEmpty()) {
map.forEach((k, v) -> multiValueMap.add(k, v));
}
return multiValueMap;
}
public static MultiValueMap convertOfString(Map<String, List<String>> map) {
MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
if (map != null && !map.isEmpty()) {
map.forEach((k, v) -> multiValueMap.addAll(k, v));
}
return multiValueMap;
}
}
请求测试
package com.hzl.controller;
import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.time.LocalDate;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @Author: zhilin
* @Date: 2023/7/8 下午4:48
* @Description: 测试类
*/
public class HttpRequestTest {
public static final String BASE_URL = "http://localhost:8090/test";
public static String url1 = BASE_URL + "/getOne";
public static String url2 = BASE_URL + "/getTwo/{name}";
public static String url3 = BASE_URL + "/getThree/{name}";
public static String url33 = BASE_URL + "/getFour/{name}";
public static String url4 = BASE_URL + "/postOne/{name}";
public static String url5 = BASE_URL + "/postTwo/{name}";
public static String url55 = BASE_URL + "/postThree/{name}";
public static void main(String[] args) {
// getTest();
postTest();
}
private static void getTest() {
HttpRequest request = new HttpRequest();
request.setUrl(url3);
request.setMethod(HttpMethod.GET.name());
request.setContentType(MediaType.APPLICATION_FORM_URLENCODED.toString());
request.setPathMap(new LinkedHashMap<>() {{
put("name", "zhangsan");
}});
request.setHeaderMap(new LinkedHashMap<>() {{
put("sa-token", Collections.singletonList("123456"));
put("day", Collections.singletonList(LocalDate.now().toString()));
}});
request.setQueryMap(new LinkedHashMap<>() {{
put("type", Collections.singletonList("java"));
put("list", Stream.of("1", "2", "3").collect(Collectors.toList()));
put("isTo", Collections.singletonList("false"));
}});
ResponseEntity responseEntity = HttpRequestUtil.getOfHeader(request);
// ResponseEntity responseEntity = HttpRequestUtil.get(request);
System.out.println("responseEntity = " + JSON.toJSONString(responseEntity));
}
private static void postTest() {
HttpRequest request = new HttpRequest();
request.setUrl(url55);
request.setMethod(HttpMethod.POST.name());
// request.setContentType(MediaType.APPLICATION_FORM_URLENCODED.toString());
request.setContentType(MediaType.APPLICATION_JSON.toString());
request.setPathMap(new LinkedHashMap<>() {{
put("name", "zhangsan");
}});
request.setHeaderMap(new LinkedHashMap<>() {{
put("sa-token", Collections.singletonList("123456"));
put("day", Collections.singletonList(LocalDate.now().toString()));
}});
request.setQueryMap(new LinkedHashMap<>() {{
put("type", Collections.singletonList("999"));
put("list", Stream.of("1", "2", "3").collect(Collectors.toList()));
put("isTo", Collections.singletonList("false"));
}});
request.setBodyMap(new LinkedHashMap<>() {{
put("hobby", Stream.of("singsong", "dance").collect(Collectors.toList()));
put("class", Collections.singletonList("1班"));
}});
String bodyJson="{\n" +
" \"body\": \"zhangsan,java,2023-07-08\",\n" +
" \"statusCode\": \"OK\",\n" +
" \"statusCodeValue\": 200\n" +
"}";
request.setBody(bodyJson);
ResponseEntity responseEntity = HttpRequestUtil.post(request);
// ResponseEntity responseEntity = HttpRequestUtil.get(request);
System.out.println("responseEntity = " + JSON.toJSONString(responseEntity));
}
}
非excahnge的常用方法请求工具类
package com.hzl.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
/**
* @Author: zhilin
* @Date: 2023/7/4 下午10:44
* @Description: TODO
*/
@Slf4j
public class RestTemplateUtil {
private static final RestTemplate restTemplate = new RestTemplate();
static {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setReadTimeout(50 * 1000);
requestFactory.setConnectTimeout(10 * 1000);
restTemplate.setRequestFactory(requestFactory);
}
// ----------------------------------GET-------------------------------------------------------
/**
* GET请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Class<T> responseType) {
return restTemplate.getForEntity(url, responseType);
}
/**
* GET请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Object... uriVariables) {
return restTemplate.getForEntity(url, responseType, uriVariables);
}
/**
* GET请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.getForEntity(url, responseType, uriVariables);
}
/**
* 带请求头的GET请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return get(url, httpHeaders, responseType, uriVariables);
}
/**
* 带请求头的GET请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Object... uriVariables) {
HttpEntity<?> requestEntity = new HttpEntity<>(headers);
return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);
}
/**
* 带请求头的GET请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return get(url, httpHeaders, responseType, uriVariables);
}
/**
* 带请求头的GET请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<?> requestEntity = new HttpEntity<>(headers);
return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);
}
// ----------------------------------POST-------------------------------------------------------
/**
* POST请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @return
*/
public static <T> ResponseEntity<T> post(String url, Class<T> responseType) {
log.info("--------------------post请求接口,地址为{}", url);
return restTemplate.postForEntity(url, HttpEntity.EMPTY, responseType);
}
/**
* POST请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType) {
log.info("--------------------post请求接口,参数为{},地址为{}", requestBody, url);
return restTemplate.postForEntity(url, requestBody, responseType);
}
/**
* POST请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Object... uriVariables) {
return restTemplate.postForEntity(url, requestBody, responseType, uriVariables);
}
/**
* POST请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
log.info("--------------------post请求接口,参数为{},地址为{},地址变量为{}", requestBody, url, uriVariables);
return restTemplate.postForEntity(url, requestBody, responseType, uriVariables);
}
/**
* 带请求头的POST请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return post(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 带请求头的POST请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return post(url, requestEntity, responseType, uriVariables);
}
/**
* 带请求头的POST请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return post(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 带请求头的POST请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return post(url, requestEntity, responseType, uriVariables);
}
/**
* 自定义请求头和请求体的POST请求调用方式
*
* @param url 请求URL
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables);
}
/**
* 自定义请求头和请求体的POST请求调用方式
*
* @param url 请求URL
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables);
}
// ----------------------------------PUT-------------------------------------------------------
/**
* PUT请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, Class<T> responseType, Object... uriVariables) {
return put(url, HttpEntity.EMPTY, responseType, uriVariables);
}
/**
* PUT请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);
return put(url, requestEntity, responseType, uriVariables);
}
/**
* PUT请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);
return put(url, requestEntity, responseType, uriVariables);
}
/**
* 带请求头的PUT请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return put(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 带请求头的PUT请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return put(url, requestEntity, responseType, uriVariables);
}
/**
* 带请求头的PUT请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return put(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 带请求头的PUT请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return put(url, requestEntity, responseType, uriVariables);
}
/**
* 自定义请求头和请求体的PUT请求调用方式
*
* @param url 请求URL
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {
return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);
}
/**
* 自定义请求头和请求体的PUT请求调用方式
*
* @param url 请求URL
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);
}
// ----------------------------------DELETE-------------------------------------------------------
/**
* DELETE请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, Class<T> responseType, Object... uriVariables) {
return delete(url, HttpEntity.EMPTY, responseType, uriVariables);
}
/**
* DELETE请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, Class<T> responseType, Map<String, ?> uriVariables) {
return delete(url, HttpEntity.EMPTY, responseType, uriVariables);
}
/**
* DELETE请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* DELETE请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 带请求头的DELETE请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return delete(url, httpHeaders, responseType, uriVariables);
}
/**
* 带请求头的DELETE请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 带请求头的DELETE请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return delete(url, httpHeaders, responseType, uriVariables);
}
/**
* 带请求头的DELETE请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 带请求头的DELETE请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return delete(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 带请求头的DELETE请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 带请求头的DELETE请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return delete(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 带请求头的DELETE请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return delete(url, requestEntity, responseType, uriVariables);
}
/**
* 自定义请求头和请求体的DELETE请求调用方式
*
* @param url 请求URL
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {
return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);
}
/**
* 自定义请求头和请求体的DELETE请求调用方式
*
* @param url 请求URL
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);
}
// ----------------------------------通用方法-------------------------------------------------------
/**
* 通用调用方式
*
* @param url 请求URL
* @param method 请求方法类型
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) {
return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables);
}
/**
* 通用调用方式
*
* @param url 请求URL
* @param method 请求方法类型
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables);
}
/**
* 获取RestTemplate实例对象,可自由调用其方法
*
* @return RestTemplate实例对象
*/
public static RestTemplate getRestTemplate() {
return restTemplate;
}
}