记一个工作中常用的 RestTemplateUtils

记录了

post请求Response泛型
post请求Response<List>泛型
post请求泛型
get 请求响应泛型对象

亲测可用,有需要的小伙伴自取哈

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.core.domain.PageResult;
import com.ruoyi.common.core.domain.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;import java.util.List;/*** @author: chenxiao* @description: resttemplate封装* @date: Create in 2022/8/31 17:27* @modified By:*/
public class RestTemplateUtils {private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateUtils.class);/*** Description: post请求Response泛型* <br />* CreateDate 2021-11-01 16:53:25** @author chenxiao**/public static <T> Response<T> postForResponse(RestTemplate restTemplate, String url, Object request,Class<T> cls, Object... urlVariables) {long startTime = System.currentTimeMillis();LOGGER.info("postForResponse url: {},request: {}", url,JSON.toJSONString(request));ResponseEntity<String> entity =restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);Response<T> result = JSON.parseObject(entity.getBody(), new TypeReference<Response<T>>(cls) {}.getType());LOGGER.info("postForResponse url: {},response: {},cost: {} ms", url,JSON.toJSONString(result),System.currentTimeMillis() - startTime);return result;}/*** Description: post请求Response泛型* <br />* CreateDate 2021-11-01 16:53:25** @author chenxiao**/public static Response postForResponse(RestTemplate restTemplate, String url, Object request,Object... urlVariables) {long startTime = System.currentTimeMillis();LOGGER.info("postForResponse url: {},request: {}", url,JSON.toJSONString(request));ResponseEntity<String> entity =restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);Response result = JSON.parseObject(entity.getBody(), Response.class);LOGGER.info("postForResponse url: {},response: {},cost: {} ms", url,JSON.toJSONString(result),System.currentTimeMillis() - startTime);return result;}/*** Description: post请求Response<PageResult>泛型* <br />* CreateDate 2021-11-01 16:53:25** @author chenxiao**/public static <T> Response<PageResult<T>> postForResponsePageResult(RestTemplate restTemplate, String url,Object request,Class<T> cls, Object... urlVariables) {long startTime = System.currentTimeMillis();LOGGER.info("postForResponsePageResult url: {},request: {}", url,JSON.toJSONString(request));ResponseEntity<String> entity =restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);Response<PageResult<T>> result = JSON.parseObject(entity.getBody(),new TypeReference<Response<PageResult<T>>>(cls) {}.getType());LOGGER.info("postForResponsePageResult url: {},response: {},cost: {} ms", url,JSON.toJSONString(result),System.currentTimeMillis() - startTime);return result;}/*** Description: post请求Response<List>泛型* <br />* CreateDate 2021-11-01 16:53:25** @author chenxiao**/public static <T> Response<List<T>> postForResponseList(RestTemplate restTemplate, String url, Object request,Class<T> cls, Object... urlVariables) {long startTime = System.currentTimeMillis();LOGGER.info("postForResponseList url: {},request: {}", url,JSON.toJSONString(request));ResponseEntity<String> entity =restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);Response<List<T>> result = JSON.parseObject(entity.getBody(), new TypeReference<Response<List<T>>>(cls) {}.getType());LOGGER.info("postForResponseList url: {},response: {},cost: {} ms", url,JSON.toJSONString(result),System.currentTimeMillis() - startTime);return result;}/*** Description: post请求泛型* <br />* CreateDate 2021-11-01 16:53:25** @author chenxiao**/public static <T> T postForObject(RestTemplate restTemplate, String url, Object request,Class<T> cls, Object... urlVariables) {long startTime = System.currentTimeMillis();LOGGER.info("postForObject url: {},request: {}", url,JSON.toJSONString(request));ResponseEntity<String> entity =restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);LOGGER.info("postForObject url: {},response: {},cost: {} ms", url,JSON.toJSONString(entity.getBody()),System.currentTimeMillis() - startTime);return JSON.parseObject(entity.getBody(), cls);}/*** Description: post请求泛型* <br />* CreateDate 2021-11-01 16:53:25** @author chenxiao**/public static <T> T postForObject(RestTemplate restTemplate, String url, HttpEntity<?> requestEntity,Class<T> cls, Object... urlVariables) {long startTime = System.currentTimeMillis();LOGGER.info("postForObject url: {},request: {}", url,JSON.toJSONString(requestEntity));ResponseEntity<String> entity =restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, urlVariables);T result = JSON.parseObject(entity.getBody(), cls);LOGGER.info("postForObject url: {},response: {},cost: {} ms", url,JSON.toJSONString(result),System.currentTimeMillis() - startTime);return result;}/*** Description: get 请求响应泛型对象* <br />* CreateDate 2022-05-13 18:41:15** @author chenxiao**/public static <T> T getForObject(RestTemplate restTemplate, String url, Class<T> cls) {long startTime = System.currentTimeMillis();LOGGER.info("getForObject url: {}", url);ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);T result = JSON.parseObject(entity.getBody(), cls);LOGGER.info("getForObject url: {},response: {},cost: {} ms", url,JSON.toJSONString(result),System.currentTimeMillis() - startTime);return result;}/*** Description: post请求Response泛型* <br />* CreateDate 2021-11-01 16:53:25** @author chenxiao**/public static <T> Response<T> getForResponse(RestTemplate restTemplate, String url, Class<T> cls) {long startTime = System.currentTimeMillis();LOGGER.info("getForResponse url: {}", url);ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);Response<T> result = JSON.parseObject(entity.getBody(), new TypeReference<Response<T>>(cls) {}.getType());LOGGER.info("getForResponse url: {},response: {},cost: {} ms", url,JSON.toJSONString(result),System.currentTimeMillis() - startTime);return result;}
}

RestTemplate封装相关推荐

  1. RestTemplate封装方法

    在使用spring的RestTemplate方法远程调用接口的时候需要自己拼接链接,这样封装一大串数据不美观,阅读性也不强,如果封装一个方法,把参数封装在Map集合里面,然后传入url和Map得到自己 ...

  2. REST访问(RestTemplate)

    https://www.cnblogs.com/softidea/p/6910198.html 经常需要发送一个GET/POST请求到其他系统(REST API),通过JDK自带的HttpURLCon ...

  3. HTTP客户端请求工具RestTemplate

    RestTemplate详解 服务端访问HTTP服务随处可见!传统情况下我们都会使用Apache的HttpClient,不过Apache的封装的API非常复杂,还得自己做二次封装和管理资源的回收,因此 ...

  4. 看顶级渣男如何邀约100个女朋友(一)

    文章目录 一.前言 二.远程调用的基础,RestTemplate封装Http请求 三.Ribbon负载均衡的使用 3.1 Ribbon负载均衡的原理 3.2 Ribbon负载均衡的实现 四.Ribbo ...

  5. Java获取天气情况的方式

    说明 经过搜集和参考网上的相关资料,Java获取天气情况数据的通用步骤如下: 调用天气接口api: 解析返回的XML 或 JSON数据: 这里我并不去用代码实现一个Demo,而是记录一下思路,以后有需 ...

  6. Http请求之优雅的RestTemplate

    前言 本篇博客为对RestTemplate总结 HttpURLConnection 在讲RestTemplate之前我们来看看再没有RestTemplate之前是怎么发送http请求的. privat ...

  7. Spring Boot 中的 RestTemplate不好用?试试 Retrofit !

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者 | 六点半起床 来源 | juejin.im/post/68 ...

  8. 详解 RestTemplate 操作

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xmt1139057136/article/details/82761642 作为开发人员,我们经常关 ...

  9. Spring boot ----RestTemplate学习笔记

    ****spring boot-----restTemplate 封装了HttpURLConnection,HttpClient,Netty等接口访问实现库 restTemplet包含以下部分 Htt ...

最新文章

  1. LeetCode刷题-8
  2. Postman收费太贵了,我决定用Postwoman...
  3. 从0到1:CTFer成长之路
  4. C#之windows桌面软件第一课:倒时器软件
  5. MemoryStream类
  6. form表单提交时,同一个名字的input类型的两个同时提交会覆盖吗
  7. 飞鸽传书开发者的圈子里面 有很多对飞鸽公司
  8. 原F1000Prime推荐:ACE2泛癌分析图谱(TCGA数据库挖掘)
  9. mysql 多列索引的生效规则,生成1000w数据的存储过程
  10. Eclisp配置Maven(基础简易版)
  11. 如何学习前端知识?优秀的前端开发工程师应该具备什么条件?
  12. apache配置多https域名对应单个证书和多个不同的https域名对应多个不同的证书
  13. Pandas——merge(合并)
  14. [PM2][ERROR] Script not found 和 npm in fork_mode
  15. Android学习资料整理:流行框架网站书籍推荐---博客推荐
  16. 17万字 | 2021密码应用技术白皮书(附下载)
  17. 解读HRP建设2.0时代:管控模型从未改变
  18. 合并时显示是无效的m3u8文件_如何合并m3u8及ts文件
  19. 微信订阅号签到功能_微信公众号积分签到功能怎么添加,怎么制作微信签到赚积分...
  20. 阿里矢量图刷新显示异常

热门文章

  1. 用lxml的xpath演示爬虫提取笑话集网页其中的标题,url,浏览数,日期,笑话内容
  2. 微信在线客服 php,微信小程序中添加联系在线客服功能
  3. yandex挑选关键词的工具
  4. 这里有一份面筋请查收(一)
  5. 服务器看门狗芯片电路图,看门狗芯片MAX708的工作原理及数 - 电源设计应用 - 电子发烧友网...
  6. 编码时,不要让字母和数字产生混淆,影响开发判断
  7. PowerPC PPC460-S MMU(三 Access Control)
  8. 挑选西瓜(决策树实现)
  9. 文件上传漏洞之——远程文件包含漏洞(RFI)
  10. 光敏电阻与光电二极管的区别