首页 > 其他分享 >resttemplate封装类方便调用

resttemplate封装类方便调用

时间:2023-05-22 13:46:45浏览次数:55  
标签:调用 封装 url request resttemplate entity JSON result new

  1 public class RnrFpRestTemplateUtils {
  2 
  3     private static final Logger LOGGER = LoggerFactory.getLogger(RnrFpRestTemplateUtils.class);
  4 
  5 
  6     /**
  7      * Description: post请求Response泛型
  8      * <br />
  9      * CreateDate 2021-11-01 16:53:25
 10      *
 11      * @author yuyi
 12      **/
 13     public static <T> Response<T> postForResponse(RestTemplate restTemplate, String url, Object request,
 14                                                   Class<T> cls, Object... urlVariables) {
 15         long startTime = System.currentTimeMillis();
 16         LOGGER.info("postForResponse url: {},request: {}", url,
 17                 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter()));
 18         ResponseEntity<String> entity =
 19                 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);
 20         Response<T> result = JSON.parseObject(entity.getBody(), new TypeReference<Response<T>>(cls) {
 21         }.getType());
 22         LOGGER.info("postForResponse url: {},response: {},cost: {} ms", url,
 23                 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()),
 24                 System.currentTimeMillis() - startTime);
 25         return result;
 26     }
 27 
 28     /**
 29      * Description: post请求Response泛型
 30      * <br />
 31      * CreateDate 2021-11-01 16:53:25
 32      *
 33      * @author yuyi
 34      **/
 35     public static Response postForResponse(RestTemplate restTemplate, String url, Object request,
 36                                            Object... urlVariables) {
 37         long startTime = System.currentTimeMillis();
 38         LOGGER.info("postForResponse url: {},request: {}", url,
 39                 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter()));
 40         ResponseEntity<String> entity =
 41                 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);
 42         Response result = JSON.parseObject(entity.getBody(), Response.class);
 43         LOGGER.info("postForResponse url: {},response: {},cost: {} ms", url,
 44                 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()),
 45                 System.currentTimeMillis() - startTime);
 46         return result;
 47     }
 48 
 49     /**
 50      * Description: post请求Response泛型
 51      * <br />
 52      * CreateDate 2021-11-01 16:53:25
 53      *
 54      * @author yuyi
 55      **/
 56     public static Response postOpenApiForResponse(RestTemplate restTemplate, String url, HttpEntity request,
 57                                                   Object... urlVariables) {
 58         long startTime = System.currentTimeMillis();
 59         LOGGER.info("postOpenApiForResponse url: {},request: {}", url,
 60                 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter()));
 61         ResponseEntity<String> entity =
 62                 restTemplate.exchange(url, HttpMethod.POST, request, String.class, urlVariables);
 63         Response result = JSON.parseObject(entity.getBody(), Response.class);
 64         LOGGER.info("postOpenApiForResponse url: {},response: {},cost: {} ms", url,
 65                 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()),
 66                 System.currentTimeMillis() - startTime);
 67         return result;
 68     }
 69 
 70     /**
 71      * Description: post请求Response<PageResult>泛型
 72      * <br />
 73      * CreateDate 2021-11-01 16:53:25
 74      *
 75      * @author yuyi
 76      **/
 77     public static <T> Response<PageResult<T>> postForResponsePageResult(RestTemplate restTemplate, String url,
 78                                                                         Object request,
 79                                                                         Class<T> cls, Object... urlVariables) {
 80         long startTime = System.currentTimeMillis();
 81         LOGGER.info("postForResponsePageResult url: {},request: {}", url,
 82                 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter()));
 83         ResponseEntity<String> entity =
 84                 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);
 85         Response<PageResult<T>> result = JSON.parseObject(entity.getBody(),
 86                 new TypeReference<Response<PageResult<T>>>(cls) {
 87                 }.getType());
 88         LOGGER.info("postForResponsePageResult url: {},response: {},cost: {} ms", url,
 89                 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()),
 90                 System.currentTimeMillis() - startTime);
 91         return result;
 92     }
 93 
 94     /**
 95      * Description: post请求Response<List>泛型
 96      * <br />
 97      * CreateDate 2021-11-01 16:53:25
 98      *
 99      * @author yuyi
100      **/
101     public static <T> Response<List<T>> postForResponseList(RestTemplate restTemplate, String url, Object request,
102                                                             Class<T> cls, Object... urlVariables) {
103         long startTime = System.currentTimeMillis();
104         LOGGER.info("postForResponseList url: {},request: {}", url,
105                 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter()));
106         ResponseEntity<String> entity =
107                 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);
108         Response<List<T>> result = JSON.parseObject(entity.getBody(), new TypeReference<Response<List<T>>>(cls) {
109         }.getType());
110         LOGGER.info("postForResponseList url: {},response: {},cost: {} ms", url,
111                 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()),
112                 System.currentTimeMillis() - startTime);
113         return result;
114     }
115 
116     /**
117      * Description: post请求泛型
118      * <br />
119      * CreateDate 2021-11-01 16:53:25
120      *
121      * @author yuyi
122      **/
123     public static <T> T postForObject(RestTemplate restTemplate, String url, Object request,
124                                       Class<T> cls, Object... urlVariables) {
125         long startTime = System.currentTimeMillis();
126         LOGGER.info("postForObject url: {},request: {}", url,
127                 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter()));
128         ResponseEntity<String> entity =
129                 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);
130         LOGGER.info("postForObject url: {},response: {},cost: {} ms", url,
131                 JSON.toJSONString(entity.getBody(), new FpLogOutputJsonSerializeFilter()),
132                 System.currentTimeMillis() - startTime);
133         return JSON.parseObject(entity.getBody(), cls);
134     }
135 
136     /**
137      * Description: post请求泛型
138      * <br />
139      * CreateDate 2021-11-01 16:53:25
140      *
141      * @author yuyi
142      **/
143     public static <T> T postOpenApiForObject(RestTemplate restTemplate, String url, HttpEntity request,
144                                              Class<T> cls, Object... urlVariables) {
145         long startTime = System.currentTimeMillis();
146         LOGGER.info("postOpenApiForObject url: {},request: {}", url,
147                 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter()));
148         ResponseEntity<String> entity =
149                 restTemplate.exchange(url, HttpMethod.POST, request, String.class, urlVariables);
150         LOGGER.info("postOpenApiForObject url: {},response: {},cost: {} ms", url,
151                 JSON.toJSONString(entity.getBody(), new FpLogOutputJsonSerializeFilter()),
152                 System.currentTimeMillis() - startTime);
153         return JSON.parseObject(entity.getBody(), cls);
154     }
155 
156     /**
157      * Description: post请求泛型
158      * <br />
159      * CreateDate 2021-11-01 16:53:25
160      *
161      * @author yuyi
162      **/
163     public static <T> T postForObject(RestTemplate restTemplate, String url, HttpEntity<?> requestEntity,
164                                       Class<T> cls, Object... urlVariables) {
165         long startTime = System.currentTimeMillis();
166         LOGGER.info("postForObject url: {},request: {}", url,
167                 JSON.toJSONString(requestEntity, new FpLogOutputJsonSerializeFilter()));
168         ResponseEntity<String> entity =
169                 restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, urlVariables);
170         T result = JSON.parseObject(entity.getBody(), cls);
171         LOGGER.info("postForObject url: {},response: {},cost: {} ms", url,
172                 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()),
173                 System.currentTimeMillis() - startTime);
174         return result;
175     }
176 
177     /**
178      * Description: get 请求响应泛型对象
179      * <br />
180      * CreateDate 2022-05-13 18:41:15
181      *
182      * @author yuyi
183      **/
184     public static <T> T getForObject(RestTemplate restTemplate, String url, Class<T> cls) {
185         long startTime = System.currentTimeMillis();
186         LOGGER.info("getForObject url: {}", url);
187         ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
188         T result = JSON.parseObject(entity.getBody(), cls);
189         LOGGER.info("getForObject url: {},response: {},cost: {} ms", url,
190                 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()),
191                 System.currentTimeMillis() - startTime);
192         return result;
193     }
194 
195     /**
196      * Description: post请求Response泛型
197      * <br />
198      * CreateDate 2021-11-01 16:53:25
199      *
200      * @author yuyi
201      **/
202     public static <T> Response<T> getForResponse(RestTemplate restTemplate, String url, Class<T> cls) {
203         long startTime = System.currentTimeMillis();
204         LOGGER.info("getForResponse url: {}", url);
205         ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
206         Response<T> result = JSON.parseObject(entity.getBody(), new TypeReference<Response<T>>(cls) {
207         }.getType());
208         LOGGER.info("getForResponse url: {},response: {},cost: {} ms", url,
209                 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()),
210                 System.currentTimeMillis() - startTime);
211         return result;
212     }
213 }
View Code

 


标签:调用,封装,url,request,resttemplate,entity,JSON,result,new
From: https://www.cnblogs.com/pengmt/p/17420377.html

相关文章

  • 【iOS开发】UIWebView调用JS点击事件(stringByEvaluatingJavaScriptFromString)
    一、场景描述产品需求是移动端app要调用h5页面,然后监听h5代码中的某个方法,最终执行h5中的具体代码。二、具体代码.m文件@interfaceViewController()<UIWebViewDelegate>@property(nonatomic,strong)UIWebView*webView;@end@implementationViewController-(void)viewDid......
  • 5-19|记录Python调用salt代码
    #放大一importsalt.clientlocal=salt.client.LocalClient()res=local.cmd(f'{minion_id}','test.ping',timeout=10)#方法二importsalt.configimportsalt.loader__opts__=salt.config.minion_config('/etc/salt/minion')__grains__......
  • iOS的JS和OC代码互相调用
    1.JS调用OC代码步骤1:以下是html页面,test1函数,并没有在页面中声明,而是在OC的UIWebView中绑定的<html><head><script>//test2(p)函数是准备OC调用JS函数用的functiontest2(p){alert(p);}</script></head><body><......
  • 构造函数的调用时机/次数
    一个变量只能调用一次构造函数,那就是在定义变量的时候调用,除非变量为局部变量,否则不会重复调用,一直到程序结束也就调用那么一次。也就是说只有在定义新的变量的时候才会调用构造函数。【eg1】定义在主函数的情况•#include<iostream>#include<windows......
  • JWT 和MD5加密工具类的封装是使用
    jwt1packagecom.atguigu.common.jwt;23importio.jsonwebtoken.*;4importorg.springframework.util.StringUtils;56importjava.util.Date;78//jwt工具类9publicclassJwtHelper{1011privatestaticlongtokenExpiration=......
  • 对Handler的一点封装
    在Android中,我们经常需要使用Handler来进行事务的异步处理。其实对于基于主线程的Handler,没有必要到处去创建。另外对于基于非主线程的Handler所对应的Thread应该在什么时候退出,始终是一个很难得抉择。因此我封装了一个基于主线程的Handler以便整个程序使用,并封......
  • KO之间互相调用
    需求假设有两个KO,命名为moduleA.KO,moduleB.KO,现在要实现在moduleB.KO中调用moduleA.KO中的函数。实现ModuleA实现源码:#include<linux/init.h>#include<linux/module.h>#include<linux/kernel.h>voidmoduleA_func(void){printk("moduleAfunction\r\n")......
  • python类中调用类方法时,报错self参数未填
    转载:(14条消息)python类中调用类方法时,报错self参数未填。_追天一方的博客-CSDN博客又碰到了一个小错误比如一个类如下:classprint_number(object):def__init__(self,string="数字是"):self.string=stringdefprint_(self,ss=3):print("{}:{}".......
  • 2023/5/21每日随笔 调用chatgpt接口实现项目的基本需要
    首先,对于我要求的工作,gpt完美胜任,那么问题来了,怎么调用chatgpt,是可以免费调用的,但需要keyword,也就得进入chatgpt官网,就得用外网,但是要它的api应用到android上,外网手段就不可取了,于是,准备冲别人搭建的平台上调用,很幸运的是,在B站上还真的找到资源,up主也很好,教我一步一实现,搭建了以......
  • odoo后台调用路由接口
    fromodoo.exceptionsimportUserErrorimportrequestsfromodoo.httpimportrequestdefaction_commit_approval(self,action_type='commit_approval'):"""action_type:commit_approval/cancel_approval......