本文主要是介绍使用 RestTemplate 发送http请求的正确姿势原来是这样的!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文要介绍的这个工具类,是基于RestTemplate
做了一层代码封装!!非常好用哦!
-
第一步添加
httpclient
依赖包
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.6</version>
</dependency>
-
然后创建一个配置,初始化
RestTemplate
@Configuration
public class HttpConfiguration {/*** 初始化RestTemplate* @return*/@ConditionalOnMissingBean(RestTemplate.class)@Beanpublic RestTemplate restTemplate(){RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());return restTemplate;}/*** 使用HttpClient作为底层客户端* @return*/private ClientHttpRequestFactory getClientHttpRequestFactory() {int timeout = 5000;RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();return new HttpComponentsClientHttpRequestFactory(client);}
}
-
接着 创建一个
HttpTemplate
类,交给Spring
管理
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;import java.net.URI;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;@Component
public class HttpTemplate {private static final Logger log = LoggerFactory.getLogger(HttpTemplate.class);@Autowiredprivate RestTemplate restTemplate;/*** get请求,返回响应实体(响应业务对象不支持范型)* 支持restful风格* @param url* @param headers* @param responseType* @param uriVariables* @param <T>* @return*/public <T> T get(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables){ResponseEntity<T> rsp = commonExchange(url, HttpMethod.GET, new HttpEntity<>(createHeaders(headers)), responseType, uriVariables);return buildResponse(rsp);}/*** get请求,返回响应实体(响应业务对象支持范型)* 支持restful风格* @param url* @param headers* @param responseType* @param uriVariables* @param <T>* @return*/public <T> T get(String url, Map<String, String> headers, ParameterizedTypeReference<T> responseType, Object... uriVariables){ResponseEntity<T> rsp = commonExchange(url, HttpMethod.GET, new HttpEntity<>(createHeaders(headers)), responseType, uriVariables);return buildResponse(rsp);}/*** post请求,form表单提交(响应业务对象不支持范型)* 支持restful风格* @param url* @param headers* @param paramMap* @param responseType* @param uriVariables* @param <T>* @return*/public <T> T postByFrom(String url, Map<String, String> headers, Map<String, Object> paramMap, Class<T> responseType, Object... uriVariables){//指定请求头为表单类型HttpHeaders httpHeaders = createHeaders(headers);httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);ResponseEntity<T> rsp = commonExchange(url, HttpMethod.POST, new HttpEntity<>(createBody(paramMap), httpHeaders), responseType, uriVariables);return buildResponse(rsp);}/*** post请求,form表单提交(响应业务对象支持范型)* 支持restful风格* @param url* @param headers* @param paramMap* @param responseType* @param uriVariables* @param <T>* @return*/public <T> T postByFrom(String url, Map<String, String> headers, Map<String, Object> paramMap, ParameterizedTypeReference<T> responseType, Object... uriVariables){//指定请求头为表单类型HttpHeaders httpHeaders = createHeaders(headers);httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);ResponseEntity<T> rsp = commonExchange(url, HttpMethod.POST, new HttpEntity<>(createBody(paramMap), httpHeaders), responseType, uriVariables);return buildResponse(rsp);}/*** post请求,json提交(响应业务对象不支持范型)* 支持restful风格* @param url* @param headers* @param request* @param responseType* @param uriVariables* @param <T>* @return*/public <T> T postByJson(String url, Map<String, String> headers, Object request, Class<T> responseType, Object... uriVariables){//指定请求头为json类型HttpHeaders httpHeaders = createHeaders(headers);httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);ResponseEntity<T> rsp = commonExchange(url, HttpMethod.POST, new HttpEntity<>(request, httpHeaders), responseType, uriVariables);return buildResponse(rsp);}/*** post请求,json提交(响应业务对象支持范型)* 支持restful风格* @param url* @param headers* @param request* @param responseType* @param uriVariables* @param <T>* @return*/public <T> T postByJson(String url, Map<String, String> headers, Object request, ParameterizedTypeReference<T> responseType, Object... uriVariables){//指定请求头为json类型HttpHeaders httpHeaders = createHeaders(headers);httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);ResponseEntity<T> rsp = commonExchange(url, HttpMethod.POST, new HttpEntity<>(request, httpHeaders), responseType, uriVariables);return buildResponse(rsp);}/*** post请求,json提交,重定项* 支持restful风格* @param url* @param headers* @param request* @param uriVariables* @return*/public String postForLocation(String url, Map<String, String> headers, Object request, Object... uriVariables){//指定请求头为json类型HttpHeaders httpHeaders = createHeaders(headers);httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);URI uri = restTemplate.postForLocation(url, new HttpEntity<>(request, httpHeaders), uriVariables);if(Objects.nonNull(uri)){return uri.toString();}return null;}/*** put请求,json提交(响应业务对象不支持范型)* @param url* @param headers* @param request* @param uriVariables*/public <T> T put(String url, Map<String, String> headers, Object request, Class<T> responseType, Object... uriVariables){//指定请求头为json类型HttpHeaders httpHeaders = createHeaders(headers);httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);ResponseEntity<T> rsp = commonExchange(url, HttpMethod.PUT, new HttpEntity<>(request, httpHeaders), responseType, uriVariables);return buildResponse(rsp);}/*** put请求,json提交(响应业务对象支持范型)* @param url* @param headers* @param request* @param uriVariables*/public <T> T put(String url, Map<String, String> headers, Object request, ParameterizedTypeReference<T> responseType, Object... uriVariables){//指定请求头为json类型HttpHeaders httpHeaders = createHeaders(headers);httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);ResponseEntity<T> rsp = commonExchange(url, HttpMethod.PUT, new HttpEntity<>(request, httpHeaders), responseType, uriVariables);return buildResponse(rsp);}/*** delete请求(响应业务对象不支持范型)* @param url* @param headers* @param uriVariables* @return*/public <T> T delete(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables){ResponseEntity<T> rsp = commonExchange(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(headers)), responseType, uriVariables);return buildResponse(rsp);}/*** delete请求(响应业务对象支持范型)* @param url* @param headers* @param uriVariables* @return*/public <T> T delete(String url, Map<String, String> headers, ParameterizedTypeReference<T> responseType, Object... uriVariables){ResponseEntity<T> rsp = commonExchange(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(headers)), responseType, uriVariables);return buildResponse(rsp);}/*** post请求,文件表单上传提交(响应业务对象不支持范型)* 支持restful风格* @param url* @param headers* @param paramMap* @param responseType* @param uriVariables* @param <T>* @return*/public <T> T uploadFile(String url, Map<String, String> headers, MultiValueMap<String, Object> paramMap, Class<T> responseType, Object... uriVariables){//指定请求头为文件&表单类型HttpHeaders httpHeaders = createHeaders(headers);httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);ResponseEntity<T> rsp = commonExchange(url, HttpMethod.POST, new HttpEntity<>(paramMap, httpHeaders), responseType, uriVariables);return buildResponse(rsp);}/*** post请求,文件表单上传提交(响应业务对象支持范型)* 支持restful风格* @param url* @param headers* @param paramMap* @param responseType* @param uriVariables* @param <T>* @return*/public <T> T uploadFile(String url, Map<String, String> headers, MultiValueMap<String, Object> paramMap, ParameterizedTypeReference<T> responseType, Object... uriVariables){//指定请求头为文件&表单类型HttpHeaders httpHeaders = createHeaders(headers);httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);ResponseEntity<T> rsp = commonExchange(url, HttpMethod.POST, new HttpEntity<>(paramMap, httpHeaders), responseType, uriVariables);return buildResponse(rsp);}/*** 下载文件* @param url* @param headers* @param uriVariables* @return*/public byte[] downloadFile(String url, Map<String, String> headers, Object... uriVariables){ResponseEntity<byte[]> rsp = commonExchange(url, HttpMethod.GET, new HttpEntity<>(createHeaders(headers)), byte[].class, uriVariables);return buildResponse(rsp);}/*** 下载大文件* @param url* @param headers* @param responseExtractor* @param uriVariables*/public void downloadBigFile(String url, Map<String, String> headers, ResponseExtractor responseExtractor, Object... uriVariables){RequestCallback requestCallback = request -> {//指定请求头信息request.getHeaders().addAll(createHeaders(headers));//定义请求头的接收类型request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));};restTemplate.execute(url, HttpMethod.GET, requestCallback,responseExtractor, uriVariables);}/*** 公共http请求方法(响应业务对象不支持范型)* @param url* @param method* @param requestEntity* @param responseType* @param uriVariables* @param <T>* @return*/public <T> ResponseEntity<T> commonExchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables){return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables);}/*** 公共http请求方法(响应业务对象支持范型)* @param url* @param method* @param requestEntity* @param responseType* @param uriVariables* @param <T>* @return*/public <T> ResponseEntity<T> commonExchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables){return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables);}/*** 封装头部参数* @param headers* @return*/private HttpHeaders createHeaders(Map<String, String> headers){return new HttpHeaders(){{if(headers != null && !headers.isEmpty()){headers.entrySet().forEach(item -> {set(item.getKey(), item.getValue());});}}};}/*** 封装请求体* @param paramMap* @return*/private MultiValueMap<String, Object> createBody(Map<String, Object> paramMap){MultiValueMap<String, Object> valueMap = new LinkedMultiValueMap<>();if(paramMap != null && !paramMap.isEmpty()){paramMap.entrySet().forEach(item -> {valueMap.add(item.getKey(), item.getValue());});}return valueMap;}/*** 返回响应对象* @param rsp* @param <T>* @return*/private <T> T buildResponse(ResponseEntity<T> rsp){if(!rsp.getStatusCode().is2xxSuccessful()){throw new RuntimeException(rsp.getStatusCode().getReasonPhrase());}return rsp.getBody();}
}
当遇到返回的对象是范型类型的时候,我们可以这样操作!
以下面这个/testPostByJsonObj
接口为例!
/*** 模拟JSON请求,post方法测试* @param request* @return*/
@RequestMapping(value = "testPostByJsonObj", method = RequestMethod.POST)
public ResponseBeanObj<ResponseBean> testPostByJsonObj(@RequestBody RequestBean requestBean,HttpServletRequest request){HttpServletRequestLog.systemLog(request);//范型测试ResponseBean responseBean = new ResponseBean();responseBean.setCode("200000");responseBean.setMsg("responseBean");//范型测试ResponseBeanObj<ResponseBean> result = new ResponseBeanObj<>();result.setCode("200");result.setMsg("请求成功,方法:testPostByJsonObj,请求参数:" + JSON.toJSONString(requestBean));result.setObj(responseBean);System.out.println(JSON.toJSONString(result));return result;
}
使用RestTemplate
工具发起网络请求,代码如下!
//将返回的范型对象包装到ParameterizedTypeReference对象里面
ParameterizedTypeReference<ResponseBeanObj<ResponseBean>> typeRef = new ParameterizedTypeReference<ResponseBeanObj<ResponseBean>>() {};//使用restTemplate发起网络请求
ResponseBeanObj<ResponseBean> responseBean = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
采用restTemplate.exchange()
方法,即可实现返回对象范型类型的反序列化!
如果使用上面封装的HttpTemplate
工具进行操作,也更简单,代码如下:
/*** 模拟JSON提交,post请求,范型返回对象测试*/
@Test
public void testPostByJsonObj(){//请求地址String url = "http://localhost:8080/testPostByJsonObj";//入参RequestBean request = new RequestBean();request.setUserName("唐三藏");request.setUserPwd("123456789");//发送post请求ParameterizedTypeReference<ResponseBeanObj<ResponseBean>> typeRef = new ParameterizedTypeReference<ResponseBeanObj<ResponseBean>>() {};//范型测试ResponseBeanObj<ResponseBean> responseBean = httpTemplate.postByJson(url, createHeader("testPostByJsonObj"), request, typeRef);System.out.println(JSON.toJSONString(responseBean));
}
在某些场景下,当你使用restTemplate
发起网络请求时,所有的请求头部需要带上统一的参数,例如Authorization
鉴权码,what can I do?
那我们可以在RestTemplate
初始化之后,添加一个拦截器,然后在拦截器的请求头部统一注入鉴权码,就可以轻松实现全局加入某个参数,方式如下!
/*** 初始化RestTemplate* @return*/
@Bean
public RestTemplate restTemplate(){RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());// 添加一个拦截器,在请求头部添加 Authorization 鉴权码restTemplate.getInterceptors().add((request, body, execution) -> {request.getHeaders().add("Authorization", "xxxxxXXXXX");return execution.execute(request, body);});return restTemplate;
}
最后说一句(求关注!别白嫖!)
如果这篇文章对您有所帮助,或者有所启发的话,求一键三连:点赞、转发、在看。
关注公众号:woniuxgg,在公众号中回复:笔记 就可以获得蜗牛为你精心准备的java实战语雀笔记,回复面试、开发手册、有超赞的粉丝福利!
这篇关于使用 RestTemplate 发送http请求的正确姿势原来是这样的!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!