package com.xjj;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import org.apache.commons.beanutils.BeanUtils;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xjj.web.controller.LoginObj; import net.minidev.json.JSONObject; public class CMERestClient {
TestRestTemplate restTemplate = new TestRestTemplate(); public <T> T processRestJson(String url, String as, Class<T> t) { TestRestTemplate restTemplate = new TestRestTemplate();
HttpEntity<String> entity = getJSONHttpEntityObj(as);
T ss = restTemplate.postForObject(url, entity, t);
return ss;
} public <T> T processRestJsonObj(String url, T as, Class<T> t) { HttpEntity<T> entity = getJSONHttpEntityObj(as);
T ss = restTemplate.postForObject(url, as, t);
return ss;
} public <T> List<T> processRestJsonList(String url, String as, Class<T> t) throws Exception { TestRestTemplate restTemplate = new TestRestTemplate();
HttpEntity<String> entity = getJSONHttpEntity(as);
List<Map> mapList = restTemplate.postForObject(url, entity, List.class); return convert(mapList, t);
} public static <T> T mapToObject(Map map, Class<T> beanClass) throws Exception { T obj = beanClass.newInstance();
BeanUtils.populate(obj, map); return obj; } public <T> List<T> convert(List<Map> mapList, Class<T> c) throws Exception { ObjectMapper b = new ObjectMapper(); List<T> s = mapList.stream().map(p -> sss(c, p)).collect(Collectors.toList()); return s;
} private <T> T sss(Class<T> c, Map p) {
T obj = null;
try {
obj = c.newInstance();
BeanUtils.populate(obj, p);
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return obj;
} public <T> List<T> processRestJsonExchange(String url, String as, ParameterizedTypeReference<List<T>> typeRef) { TestRestTemplate restTemplate = new TestRestTemplate();
HttpEntity<String> entity = getJSONHttpEntity(as); ResponseEntity<List<T>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, typeRef);
List<T> myModelClasses = responseEntity.getBody(); return myModelClasses;
} public JSONObject processRestJson2(String url, String as) { TestRestTemplate restTemplate = new TestRestTemplate();
HttpEntity<String> entity = getJSONHttpEntity(as);
JSONObject ss = restTemplate.postForObject(url, entity, JSONObject.class);
return ss;
} public String createJSONParm(Map<String, String> a) { ObjectMapper b = new ObjectMapper();
String as = null;
try {
as = b.writeValueAsString(a);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return as;
} public <T> String objToJSONString(T t) { ObjectMapper b = new ObjectMapper();
String as = null;
try {
as = b.writeValueAsString(t);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return as;
} public <T> HttpEntity<T> getJSONHttpEntity(T as) { HttpHeaders headers2 = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers2.setContentType(type);
headers2.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<T> entity = new HttpEntity<T>(as, headers2);
return entity;
} public <T> HttpEntity<T> getJSONHttpEntityObj(T as) { HttpHeaders headers2 = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers2.setContentType(type);
headers2.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<T> entity = new HttpEntity<T>(as, headers2);
return entity;
}
}

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. 记录一次axios请求造成的数组初始化失败

    axios请求是一个异步的请求,简单来讲就是在做其他事情的时候可以把这个先放一边等其他的事情做完后再来做这件事件. 我之前这样调用了一个方法: mounted() { this.first() thi ...

  2. Spring(转载二)

    在网上看到一篇文章,感觉写得挺不错的,转载一下,本文转载自:http://blog.csdn.net/m13666368773/article/details/7802126 一. IoC理论的背景 ...

  3. C++报错

    编译的时候会报如下错误:warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable ...

  4. BootStrap的table技术小结:数据填充、分页、列宽可拖动

    本文结构:先说明,后代码.拷贝可直接运行. 一.demo结构: 二.文件引入 这些里面除了下面2个比较难找,其他的都很好找 bootstrap-table-resizable.js colResiza ...

  5. javascript中缺少分号结尾的情况

    首先看一段代码 function* fib (max) { let a = 0 let b = 1 let n = 1 while (n < max) { yield a; [a, b] = [ ...

  6. 引擎设计跟踪(九.14.3.2) Deferred shading的后续实现和优化

    最近完成了deferred shading和spot light的支持, 并作了一部分优化. 之前forward shading也只支持方向光, 现在也支持了点光源和探照光. 对于forward sh ...

  7. 芯灵思Sinlinx A64 开发板移植SQLite3

    开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 开发板详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 首先到 http://ww ...

  8. Linux----------httpd的简介和安装及使用

    目录 一.httpd的简介 二.常用httpd版本特性 (1)httpd-2.2 (2)httpd-2.4 三.httpd的工作模型 四.httpd的程序环境即配置文件和重要目录 1.配置文件和重要目 ...

  9. securecrt-active

    Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.7 转自 http://bbs.weiphone.com/read-htm-tid-6939481.html 继续更新到7.3.2的破 ...

  10. PlantUml 使用代码画各种图

    资源 网址 官方github https://github.com/plantuml/plantuml 官方网站 http://plantuml.com/zh/ mac 下,webstorm 中使用 ...