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. postman导入csv文件,批量运行

    1.设置csv文件,第一行必须标明变量名 2.postman参数化设置 3.批量 run即可

  2. C#实现按键计算器功能2(增强版)

    1.   实验目的 (  1)熟悉C#语言的使用和语法知识 2.   实验要求 (1)设计简单的含交互界面的计算器软件,具有较强的用户体验感. (2)使用C#语言进行编程,创建窗体应用程序. (3)实 ...

  3. js中substr、substring、slice的区别

    substr(start, length) substring(from, to) slice(from, to) 以上函数只传一个参数时,认为是起始位置,然后按照正方向截取 substring的参数 ...

  4. vue day4 table

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. Linux----------nfs服务器的搭建及常识

    一.nfs简介 nfs(network file system)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源. nfs主要适用用linu ...

  6. Ansible 批量修改密码

    客户要求每3个月修改一次主机密码.密码规则为客服提供的一串字符 xxxx + 主机后3位. 将需要登录主机添加到 Ansible. 将需要登录主机的公钥添加到 known_hosts ssh-keys ...

  7. 1.搭建Angular2项目

    简述:搭建angular2的开发环境,网上已经有许多教程,不过都是window系统下的教程,我本人使用的是linux系统,搭建环境的过程也稍微比前者麻烦了一点,可参考本人的另一篇文章Linux系统下安 ...

  8. 基于javaMail的邮件发送--excel作为附件

    基于JavaMail的Java邮件发送 Author xiuhong.chen@hand-china.com Desc 简单邮件发送 Date 2017/12/8 项目中需要根据物料资质的状况实时给用 ...

  9. 新安装mysql,如何提升mysql安全性

    1.修改mysql默认端口,将3306修改为其他端口. 2.设定足够复杂的密码策略并指定访问IP(在user表中可以指定用户可访问的访问IP地址). 3.设定IP访问白名单. 4.设定root用户只能 ...

  10. postman插件部分Header设置无效的解决办法

    在使用chrome的postman插件模拟http请求的时候,碰到了设置的部分Headers无效的问题,比如说Referer设置后就无效,经过查询发现了问题原因,原因的具体说明参考postman官网的 ...