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 ...
随机推荐
- 遇到短信轰炸,别人换ip调你的短信接口怎么办
前端开发者很容易暴露自己的请求地址和参数,我们都知道,一个h5页面,按 F12 是可以看到页面的源码的,所以经常很多人会利用这一点恶意调取别人的接口. 我们公司出现了好多次短信接口被大量调用,导致一天 ...
- Codeforces 1105C: Ayoub and Lost Array(递推)
time limit per test: 1 second memory limit per test: 256 megabytes input: standard input output: sta ...
- Python学习笔记,day4
Python学习第四天 一.装饰器 函数调用顺序: 其他高级语言类似,Python 不允许在函数未声明之前,对其进行引用或者调用 高阶函数: 满足下列条件之一就可成函数为高阶函数 某一函数当做参数传入 ...
- Spring MVC中一般类使用service
在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...
- 【OO学习】OO第二单元作业总结
OO第二单元作业总结 在第二单元作业中,我们通过多线程的手段实现了电梯调度,前两次作业是单电梯调度,第三次作业是多电梯调度.这个单元中的性能分要求是完成所有请求的时间最短,因此在简单实现电梯调度的基础 ...
- 【frame系列标签】
html框架标签1.内嵌框架 <frame></frame> 在页面上开辟一块空间 frame内部属性: src 要填充的图片或者网址 width height target= ...
- 学习笔记CB007:分词、命名实体识别、词性标注、句法分析树
中文分词把文本切分成词语,还可以反过来,把该拼一起的词再拼到一起,找到命名实体. 概率图模型条件随机场适用观测值条件下决定随机变量有有限个取值情况.给定观察序列X,某个特定标记序列Y概率,指数函数 e ...
- 网络操作基础(two)
P106 一.什么是活动目录?活动目录有哪些优点? 二.什么是域.域树.森林? 三.什么是信任?什么是域的方向及传递性? 四.如何管理活动目录的信任与站点? 解答! (一) 1.活动目录:提供了用于存 ...
- springboot入门1
1引入springboot父依赖,和 spring-boot-starter-web的启动器 依赖引入后jar包展示依赖的情况 入门工程 配置数据源 package com.boot.web.con ...
- 服务程序在c#中的写法
1.在VS.NET2003中新建一个WINDOWS服务程序的项目WinSrv_A. 2.更改SERVICE1.CS属性SERVICENAME为你所要建立的服务名称,在服务管理器->名称中你可以看 ...