博主之前经常对接一些接口,所以发现写一些http请求比较麻烦,学习springboot的过程知道可以用RestTemplate
来做http请求,RestTemplate
是Spring Framework
框架封装的基于模板方法设计模式的一个工具类,带有同步模板方法 API 的原始 Spring REST
客户端类,下面博主分析一些对接过程的一些经验,RestTemplate基本使用可以参考官网文档:https://docs.spring.io/spring-framework/docs/5.1.6.RELEASE/spring-framework-reference/integration.html#rest-client-access
下面分析一些例子,先要进行环境准备:
开发环境
JDK 1.8
SpringBoot2.2.1
Maven 3.2+
开发工具
IntelliJ IDEA
smartGit
Navicat15
在IDEA里集成阿里的https://start.aliyun.com
,创建一个Spring Initializr
项目:
选择jdk版本,和maven打包方式
选择需要的dependencies
项目建好之后,可以对RestTemplate进行一些自定义的设置,比如可以拓展一下ClientHttpRequestInterceptor
,做一些自己的拦截,打印一些日志
package com.example.resttemplate.configuration;
import cn.hutool.core.convert.Convert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
@Slf4j
public class CustomClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
trackRequest(httpRequest , bytes);
ClientHttpResponse httpResponse = clientHttpRequestExecution.execute(httpRequest , bytes);
trackResponse(httpResponse);
return httpResponse;
}
private void trackRequest(HttpRequest httpRequest, byte[] bytes) {
log.info("=======================request start=================================================");
log.info("Headers : {}", httpRequest.getHeaders());
log.info("Request URI : {}", httpRequest.getURI());
log.info("Request Method : {}", httpRequest.getMethod());
log.info("Request Body : {}" , Convert.toStr(bytes));
log.info("=======================request end=================================================");
}
private void trackResponse(ClientHttpResponse httpResponse) throws IOException {
log.info("=======================response start=================================================");
log.info("Status code : {}", httpResponse.getStatusCode());
log.info("Status text : {}", httpResponse.getStatusText());
log.info("Headers : {}", httpResponse.getHeaders());
log.info("=======================response end=================================================");
}
}
自定义RestTemplateCustomizer
类:
package com.example.resttemplate.configuration;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.web.client.RestTemplate;
public class CustomRestTemplateCustomizer implements RestTemplateCustomizer {
@Override
public void customize(RestTemplate restTemplate) {
restTemplate.getInterceptors().add(new CustomClientHttpRequestInterceptor());
}
}
写一个RestTemplate的配置类,注意需要加上RestTemplate
的@Bean
,我们在项目里才能使用@Autowired RestTemplate restTemplate;
直接调用,@DependsOn
是一个Spring框架底层的注解,会禁用RestTemplate
默认的RestTemplateBuilder
类,可以不加上
package com.example.resttemplate.configuration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfiguration {
@Bean
public CustomRestTemplateCustomizer customRestTemplateCustomizer() {
return new CustomRestTemplateCustomizer();
}
@Bean
@DependsOn(value = {"customRestTemplateCustomizer"})
public RestTemplateBuilder restTemplateBuilder() {
return new RestTemplateBuilder(customRestTemplateCustomizer());
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
//单位为ms
factory.setReadTimeout(5000);
//单位为ms
factory.setConnectTimeout(5000);
return factory;
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder ,ClientHttpRequestFactory factory ) {
RestTemplate restTemplate = builder.build();
restTemplate.setRequestFactory(factory);
return restTemplate;
}
}
下面给出一个简单的例子,先写一个用户dto类:
package com.example.resttemplate.model;
import lombok.Data;
@Data
public class UserDto {
private Integer id;
private String login;
private String name;
private String company;
private String location;
private String email;
}
通过restTemplate
直接请求GitHub的用户api接口,写法是不是比自己写http的工具类,简洁很多了?
package com.example.resttemplate.controller;
import com.example.resttemplate.model.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class SampleController {
@Autowired
RestTemplate restTemplate;
@GetMapping(value = "/users/{login}")
public UserDto getUser(@PathVariable("login")String login) {
String url = new StringBuilder()
.append("https://api.github.com/users/")
.append(login)
.toString();
UserDto userDto = restTemplate.getForObject(url , UserDto.class , "");
return userDto;
}
}
在Linux里调用接口curl http://127.0.0.1:8080/users/mojombo
getForEntity
的例子,返回ResponseEntity
MultiValueMap<String, Object> uriVariables = new LinkedMultiValueMap<String, Object>();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(apprWritUrl, String.class, uriVariables);
logger.info("statusCode:{},responBody:{}", responseEntity.getStatusCode().value(), responseEntity.getBody());
if (responseEntity.getStatusCode().value() == 200) {
logger.info("调用成功!");
// 业务处理
}
postForEntity
的例子
HttpHeaders headers = new HttpHeaders();
MultiValueMap<String,Object> params = new LinkedMultiValueMap<String, Object>(16) ;
params.set("attachId",attachSeq);
params.set("seq",seq);
HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity<MultiValueMap<String,Object>>(params, headers);
ResponseEntity<String> entity = restTemplate.postForEntity("", requestEntity, String.class);
也可以使用restTemplate.exchange
的方法,这个方法需要指定请求方式,比如HttpMethod.GET
final String reqUrl = "url";
HttpEntity<String> httpEntity = this.getHttpEntity();
ResponseEntity<String> responseEntity = restTemplate.exchange(reqUrl, HttpMethod.GET , httpEntity, String.class);
logger.info("statusCode:{},responseBody:{}", responseEntity.getStatusCode().value(), responseEntity.getBody());
上传文件的例子,需要使用FileSystemResource
封装一下
RestTemplate restTemplate = new RestTemplate();
FileSystemResource resource = new FileSystemResource(new File("D:\\test.txt"));
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
param.add("file", resource);
param.add("fileName", "test.txt");
Object object = restTemplate.postForObject("http://127.0.0.1:8101/api/attachment/upload", param, Object.class);
对接有时候需要在请求头加很多校验参数,现在比较流行的是在网关里校验,下面是封装的方法,可以参考
/**
* 构建HttpEntity
* @Author nicky
* @Date 2020/10/21 11:36
* @Param []
* @return org.springframework.http.HttpEntity<java.lang.String>
*/
protected HttpEntity<String> getHttpEntity() throws Exception {
final String passToken = "passToken";
long now = new Date().getTime();
String timestamp = Long.toString((long) Math.floor(now / 1000));
String nonce = Long.toHexString(now) + "-" + Long.toHexString((long) Math.floor(Math.random() * 0xFFFFFF));
String signature= Signature.toSHA256(timestamp + passToken + nonce + timestamp);
HttpHeaders headers = new HttpHeaders();
headers.add("appid", "appid");
headers.add("serviceId","serviceId");
headers.add("signature", signature);
headers.add("timestamp", timestamp);
headers.add("nonce", nonce);
headers.add("Content-Type", "application/json");
headers.add("Cache-Control", "no-cache");
HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
return httpEntity;
}
有时候对接,需要在postman里,很多参数都是动态的,我们可以写postman得一些脚本来测试
脚本参考,对接这个时间戳timestamp
有时需要注意一些,要确定是毫秒的还是秒的的,参考博客:java时间戳 10位和13位分别是怎么来的?
var timestamp = (Date.now()/1000).toFixed()
var token=''
var nonce= Math.floor(Math.random() * 10000000000)
var appid=''
var signature = CryptoJS.SHA256(timestamp + token + nonce + timestamp).toString(CryptoJS.enc.Hex).toUpperCase()
pm.globals.set("variable_key", "variable_value");
postman.setGlobalVariable("signature", signature);
postman.setGlobalVariable("timestamp", timestamp);
postman.setGlobalVariable("nonce", nonce);
postman.setGlobalVariable("appid", appid);
博主对接过一些接口,需要一些签名加密的,下面给出一些工具类,参考,提高联调速度
import java.security.MessageDigest;
/**
* <pre>
* sha256签名算法
* </pre>
*/
public class Signature {
public static String toSHA256(String str) throws Exception {
MessageDigest messageDigest;
String encodeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
encodeStr = byte2Hex(messageDigest.digest());
} catch (Exception e) {
throw e;
}
return encodeStr;
}
// byte转换成16进制
protected static String byte2Hex(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}
}
sha1加密,这个是对接企业微信时候用到
/**
* sha1加密 <br>
* @Author nicky
* @Date 2021/04/26 10:22
* @Param [str]
* @return java.lang.String
*/
public static String sha1Digest(String str) {
try {
// SHA1签名生成
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] digest = md.digest();
StringBuffer hexstr = new StringBuffer();
String shaHex = "";
for (int i = 0; i < digest.length; i++) {
shaHex = Integer.toHexString(digest[i] & 0xFF);
if (shaHex.length() < 2) {
hexstr.append(0);
}
hexstr.append(shaHex);
}
return hexstr.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* <h2>对所有待签名参数按照字段名的ASCII 码从小到大排序</h2>
* @Author nicky
* @Date 2021/04/25 20:22
* @Param [params]
* @return java.lang.String
*/
public static String sortSignByASCII(SortedMap<String , String> parameters) {
// 以k1=v1&k2=v2...方式拼接参数
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, String> s : parameters.entrySet()) {
String k = s.getKey();
String v = s.getValue();
// 过滤空值
if (StringUtils.isBlank(v)) {
continue;
}
builder.append(k).append("=").append(v).append("&");
}
if (!parameters.isEmpty()) {
builder.deleteCharAt(builder.length() - 1);
}
return builder.toString();
}
网上复制的工具类,稍作修改
package com.example.resttemplate.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Slf4j
@Component
public class RestTemplateUtils {
@Autowired
@Qualifier("restTemplate")
private RestTemplate restTemplate;
/**
* GET请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> get(String url, Class<T> responseType) throws RestClientException{
return restTemplate.getForEntity(url, responseType);
}
/**
* GET请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> get(String url, Class<T> responseType, Object... uriVariables) throws RestClientException{
return restTemplate.getForEntity(url, responseType, uriVariables);
}
/**
* GET请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> get(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException{
return restTemplate.getForEntity(url, responseType, uriVariables);
}
/**
* 带请求头的GET请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
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 <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请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @return
*/
public <T> ResponseEntity<T> post(String url, Class<T> responseType) throws RestClientException{
return restTemplate.postForEntity(url, HttpEntity.EMPTY, responseType);
}
/**
* POST请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType) throws RestClientException {
return restTemplate.postForEntity(url, requestBody, responseType);
}
/**
* POST请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Object... uriVariables) throws RestClientException{
return restTemplate.postForEntity(url, requestBody, responseType, uriVariables);
}
/**
* POST请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
return restTemplate.postForEntity(url, requestBody, responseType, uriVariables);
}
/**
* 带请求头的POST请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType,
Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType,
Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType,
Map<String, ?> uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType,
Map<String, ?> uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType,
Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType
, Map<String, ?> uriVariables) throws RestClientException {
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables);
}
/**
* PUT请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> put(String url, Class<T> responseType, Object... uriVariables) throws RestClientException{
return put(url, HttpEntity.EMPTY, responseType, uriVariables);
}
/**
* PUT请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
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 <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType,
Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType,
Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType,
Map<String, ?> uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType,
Map<String, ?> uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType,
Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType,
Map<String, ?> uriVariables) throws RestClientException {
return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);
}
/**
* DELETE请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> delete(String url, Class<T> responseType, Object... uriVariables) throws RestClientException{
return delete(url, HttpEntity.EMPTY, responseType, uriVariables);
}
/**
* DELETE请求调用方式
*
* @param url 请求URL
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> delete(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException{
return delete(url, HttpEntity.EMPTY, responseType, uriVariables);
}
/**
* DELETE请求调用方式
*
* @param url 请求URL
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType,
Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType
, Map<String, ?> uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType,
Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType
, Object... uriVariables) throws RestClientException {
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 <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType
, Map<String, ?> uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType
, Map<String, ?> uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody,
Class<T> responseType, Object... uriVariables)throws RestClientException {
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 <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody,
Class<T> responseType, Object... uriVariables) throws RestClientException{
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 <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody,
Class<T> responseType, Map<String, ?> uriVariables)throws RestClientException {
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 <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody,
Class<T> responseType, Map<String, ?> uriVariables)throws RestClientException {
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 <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType,
Object... uriVariables)throws RestClientException {
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 <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType,
Map<String, ?> uriVariables) throws RestClientException{
return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);
}
/**
* 通用调用方式
*
* @param url 请求URL
* @param method 请求方法类型
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
Class<T> responseType, Object... uriVariables)throws RestClientException {
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 <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
Class<T> responseType, Map<String, ?> uriVariables)throws RestClientException {
return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://smilenicky.blog.csdn.net/article/details/108831497
内容来源于网络,如有侵权,请联系作者删除!