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. hightcharts详细教程

    1.初始化highcharts var chart = Highcharts.chart('container', options); 2.options 自定义图表的配置项 const option ...

  2. 我的代码-test models

    # coding: utf-8 # In[2]: import pandas as pdimport numpy as npfrom sklearn.preprocessing import bina ...

  3. 5. Web vulnerability scanners (网页漏洞扫描器 20个)

    5. Web vulnerability scanners (网页漏洞扫描器 20个) Burp Suite是攻击Web应用程序的集成平台. 它包含各种工具,它们之间有许多接口,旨在方便和加快攻击应用 ...

  4. python实现redis分布式锁

    https://www.cnblogs.com/wangwei916797941/p/10030805.html

  5. 【linux日常】 ACL权限管理

    ACL ((Access Control Lists) setfacl命令 这里引用一个非常详细的命令介绍. 要点: getfacl -R        递归获取acl权限,可以存储为文件以备还原 g ...

  6. MySQL数据库使用规范

    一.建表规约 1.[强制]表达是与否概念的字段,必须使用is_xxx的方式命名,数据类型是unsigned tinyint (1表示是,0表示否) 说明:任何字段如果为非负数,必须是unsigned ...

  7. Python之 Virtualenv简明教程

    virtualenv通过创建独立Python开发环境的工具, 来解决依赖.版本以及间接权限 问题. 比如一个项目依赖Django1.3 而当前全局开发环境为Django1.7, 版本跨度过大, 导致不 ...

  8. log4j-over-slf4j和slf4j-log4j12冲突问题解决

    解决办法: 两个jar包会循环引用导致内存溢出.解决的办法就是将两个jar包其中一个的依赖移除掉

  9. 剑指offer 6.查找和排序 旋转数组的最小数字

    题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...

  10. laravel学习资料

    http://blog.csdn.net/qq_20873039/article/category/6246852   --laravel核心概念 /Eloquent ORM / laravel bl ...