resttemlate
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的更多相关文章
- Spring cloud 之Ribbon(一)基本使用
简介 Spring cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它是基于Netflix的Riboon实现的.Ribbon是客户端负载均衡器,这有别语例如Nginx服务端负载 ...
- 微服务之springCloud-docker-feign(四)
简介 上一节,我们讨论了怎么通过,restTemlate调用cloud的生产者,实现起来还是比较复杂的,尤其是在消费复杂的Restful服务的时候,还需要进行一系列的转换,编解码等,使用Feign就完 ...
- 微服务之springCloud-docker-comsumer(三)
简介 上一节,我们讲了创建spring cloud生产者,并利用docker-compose部署到swarm集群中,这节我们讨论一下最restTemlate调用生产者服务 一.创建模块(micros ...
- SpringCloud-创建服务消费者-Ribbon方式(附代码下载)
场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
随机推荐
- postman设置环境变量
postman属于一键式安装,不多赘述 1.设置环境变量 点击设置进入 添加环境变量 添加成功可选择 应用{{}}包住变量名即可 地址变化更换即可
- ideal的maven工程启动时老是报错,提示web.xml里面的监听器找不到,但是实际又是存在的
-X clean compile package -Dmaven.repo.local=D:\repository-pss -Dmaven.test.skip=true maven仓库地址
- python rabbitMQ 发送端和接收端广播模式。
消费者模型: import pika,time consumer = pika.BlockingConnection\ (pika.ConnectionParameters('localhost')) ...
- 添加本地jar包到maven仓库
mvn install:install-file -Dfile=D:\video-lib\log4j-.jar -DgroupId=video -DartifactId=log4j -Dversion ...
- admin-7
Admin07 root tmooc 还原三台虚拟机[root@room9pc13 ~]# rht-vmctl reset classroom[root@room9pc13 ~]# rht-vmctl ...
- centos7进单用户
当重启linux系统,进入系统选择页面的时候,按e 在linux16那一行最后面添加 init=/bin/sh 按ctrl+c 挂载根分区,可读写 mount / -o rw, remount
- 温度转换 II
描述 温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fabrenheit). 请编写程序将用户输入华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度. 转换算法如下:(C表示摄氏度.F表示 ...
- return,break,continue三者区别
详解:http://www.cnblogs.com/yangdabao/p/6172210.html return:直接结束这个方法,后面所有代码不再执行,不管循坏外,还是循环内,全部停止,直接返回 ...
- Java 静态代码的作用
public student{ private static int MAXNUM=100; static{ System.out.println(MAXNUM); } student(){ Syst ...
- ActiveMQ (二)—发布订阅模式
ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...