• 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;
        }
    }
  • 相关阅读:
    领扣(LeetCode)七进制数 个人题解
    ie固定table单元格宽度
    js 阻止冒泡
    在jsp页面下, 让eclipse完全支持HTML/JS/CSS智能提示(转)
    WebStorm 6.0 与 7.0 注册码
    统制Highcharts中x轴和y轴坐标值的密度
    ie版本
    flash透明 处于最低
    eclipse svn --
    jquery---- 数组根据值进行删除
  • 原文地址:https://www.cnblogs.com/wblade/p/resttemplate.html
Copyright © 2020-2023  润新知