1. package com.xjj;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.stream.Collectors;
  6.  
  7. import org.apache.commons.beanutils.BeanUtils;
  8. import org.springframework.boot.test.web.client.TestRestTemplate;
  9. import org.springframework.core.ParameterizedTypeReference;
  10. import org.springframework.http.HttpEntity;
  11. import org.springframework.http.HttpHeaders;
  12. import org.springframework.http.HttpMethod;
  13. import org.springframework.http.MediaType;
  14. import org.springframework.http.ResponseEntity;
  15.  
  16. import com.fasterxml.jackson.core.JsonProcessingException;
  17. import com.fasterxml.jackson.databind.ObjectMapper;
  18. import com.xjj.web.controller.LoginObj;
  19.  
  20. import net.minidev.json.JSONObject;
  21.  
  22. public class CMERestClient {
  23. TestRestTemplate restTemplate = new TestRestTemplate();
  24.  
  25. public <T> T processRestJson(String url, String as, Class<T> t) {
  26.  
  27. TestRestTemplate restTemplate = new TestRestTemplate();
  28. HttpEntity<String> entity = getJSONHttpEntityObj(as);
  29. T ss = restTemplate.postForObject(url, entity, t);
  30. return ss;
  31. }
  32.  
  33. public <T> T processRestJsonObj(String url, T as, Class<T> t) {
  34.  
  35. HttpEntity<T> entity = getJSONHttpEntityObj(as);
  36. T ss = restTemplate.postForObject(url, as, t);
  37. return ss;
  38. }
  39.  
  40. public <T> List<T> processRestJsonList(String url, String as, Class<T> t) throws Exception {
  41.  
  42. TestRestTemplate restTemplate = new TestRestTemplate();
  43. HttpEntity<String> entity = getJSONHttpEntity(as);
  44. List<Map> mapList = restTemplate.postForObject(url, entity, List.class);
  45.  
  46. return convert(mapList, t);
  47. }
  48.  
  49. public static <T> T mapToObject(Map map, Class<T> beanClass) throws Exception {
  50.  
  51. T obj = beanClass.newInstance();
  52. BeanUtils.populate(obj, map);
  53.  
  54. return obj;
  55.  
  56. }
  57.  
  58. public <T> List<T> convert(List<Map> mapList, Class<T> c) throws Exception {
  59.  
  60. ObjectMapper b = new ObjectMapper();
  61.  
  62. List<T> s = mapList.stream().map(p -> sss(c, p)).collect(Collectors.toList());
  63.  
  64. return s;
  65. }
  66.  
  67. private <T> T sss(Class<T> c, Map p) {
  68. T obj = null;
  69. try {
  70. obj = c.newInstance();
  71. BeanUtils.populate(obj, p);
  72. return obj;
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. return obj;
  77. }
  78.  
  79. public <T> List<T> processRestJsonExchange(String url, String as, ParameterizedTypeReference<List<T>> typeRef) {
  80.  
  81. TestRestTemplate restTemplate = new TestRestTemplate();
  82. HttpEntity<String> entity = getJSONHttpEntity(as);
  83.  
  84. ResponseEntity<List<T>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, typeRef);
  85. List<T> myModelClasses = responseEntity.getBody();
  86.  
  87. return myModelClasses;
  88. }
  89.  
  90. public JSONObject processRestJson2(String url, String as) {
  91.  
  92. TestRestTemplate restTemplate = new TestRestTemplate();
  93. HttpEntity<String> entity = getJSONHttpEntity(as);
  94. JSONObject ss = restTemplate.postForObject(url, entity, JSONObject.class);
  95. return ss;
  96. }
  97.  
  98. public String createJSONParm(Map<String, String> a) {
  99.  
  100. ObjectMapper b = new ObjectMapper();
  101. String as = null;
  102. try {
  103. as = b.writeValueAsString(a);
  104. } catch (JsonProcessingException e) {
  105. // TODO Auto-generated catch block
  106. e.printStackTrace();
  107. }
  108. return as;
  109. }
  110.  
  111. public <T> String objToJSONString(T t) {
  112.  
  113. ObjectMapper b = new ObjectMapper();
  114. String as = null;
  115. try {
  116. as = b.writeValueAsString(t);
  117. } catch (JsonProcessingException e) {
  118. // TODO Auto-generated catch block
  119. e.printStackTrace();
  120. }
  121. return as;
  122. }
  123.  
  124. public <T> HttpEntity<T> getJSONHttpEntity(T as) {
  125.  
  126. HttpHeaders headers2 = new HttpHeaders();
  127. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  128. headers2.setContentType(type);
  129. headers2.add("Accept", MediaType.APPLICATION_JSON.toString());
  130.  
  131. HttpEntity<T> entity = new HttpEntity<T>(as, headers2);
  132. return entity;
  133. }
  134.  
  135. public <T> HttpEntity<T> getJSONHttpEntityObj(T as) {
  136.  
  137. HttpHeaders headers2 = new HttpHeaders();
  138. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  139. headers2.setContentType(type);
  140. headers2.add("Accept", MediaType.APPLICATION_JSON.toString());
  141.  
  142. HttpEntity<T> entity = new HttpEntity<T>(as, headers2);
  143. return entity;
  144. }
  145. }

resttemlate的更多相关文章

  1. Spring cloud 之Ribbon(一)基本使用

    简介 Spring cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它是基于Netflix的Riboon实现的.Ribbon是客户端负载均衡器,这有别语例如Nginx服务端负载 ...

  2. 微服务之springCloud-docker-feign(四)

    简介 上一节,我们讨论了怎么通过,restTemlate调用cloud的生产者,实现起来还是比较复杂的,尤其是在消费复杂的Restful服务的时候,还需要进行一系列的转换,编解码等,使用Feign就完 ...

  3. 微服务之springCloud-docker-comsumer(三)

    简介  上一节,我们讲了创建spring cloud生产者,并利用docker-compose部署到swarm集群中,这节我们讨论一下最restTemlate调用生产者服务 一.创建模块(micros ...

  4. SpringCloud-创建服务消费者-Ribbon方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

随机推荐

  1. 遇到短信轰炸,别人换ip调你的短信接口怎么办

    前端开发者很容易暴露自己的请求地址和参数,我们都知道,一个h5页面,按 F12 是可以看到页面的源码的,所以经常很多人会利用这一点恶意调取别人的接口. 我们公司出现了好多次短信接口被大量调用,导致一天 ...

  2. Codeforces 1105C: Ayoub and Lost Array(递推)

    time limit per test: 1 second memory limit per test: 256 megabytes input: standard input output: sta ...

  3. Python学习笔记,day4

    Python学习第四天 一.装饰器 函数调用顺序: 其他高级语言类似,Python 不允许在函数未声明之前,对其进行引用或者调用 高阶函数: 满足下列条件之一就可成函数为高阶函数 某一函数当做参数传入 ...

  4. Spring MVC中一般类使用service

    在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...

  5. 【OO学习】OO第二单元作业总结

    OO第二单元作业总结 在第二单元作业中,我们通过多线程的手段实现了电梯调度,前两次作业是单电梯调度,第三次作业是多电梯调度.这个单元中的性能分要求是完成所有请求的时间最短,因此在简单实现电梯调度的基础 ...

  6. 【frame系列标签】

    html框架标签1.内嵌框架 <frame></frame> 在页面上开辟一块空间 frame内部属性: src 要填充的图片或者网址 width height target= ...

  7. 学习笔记CB007:分词、命名实体识别、词性标注、句法分析树

    中文分词把文本切分成词语,还可以反过来,把该拼一起的词再拼到一起,找到命名实体. 概率图模型条件随机场适用观测值条件下决定随机变量有有限个取值情况.给定观察序列X,某个特定标记序列Y概率,指数函数 e ...

  8. 网络操作基础(two)

    P106 一.什么是活动目录?活动目录有哪些优点? 二.什么是域.域树.森林? 三.什么是信任?什么是域的方向及传递性? 四.如何管理活动目录的信任与站点? 解答! (一) 1.活动目录:提供了用于存 ...

  9. springboot入门1

    1引入springboot父依赖,和 spring-boot-starter-web的启动器 依赖引入后jar包展示依赖的情况 入门工程  配置数据源 package com.boot.web.con ...

  10. 服务程序在c#中的写法

    1.在VS.NET2003中新建一个WINDOWS服务程序的项目WinSrv_A. 2.更改SERVICE1.CS属性SERVICENAME为你所要建立的服务名称,在服务管理器->名称中你可以看 ...