zoukankan      html  css  js  c++  java
  • RestClientUtils

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Component;
    import org.springframework.web.client.RestTemplate;
    
    import javax.annotation.PostConstruct;
    import java.util.Map;
    
    /**
     * REST接口客户端工具类
     * 
     * @author yy
     * @date 2018年2月13日
     */
    @Component
    public class RestClientUtils {
    
        @Autowired
        private RestTemplate restTemplate;
        private static RestClientUtils restClientUtils;
    
        @PostConstruct
        public void init() {
            restClientUtils = this;
            restClientUtils.restTemplate = this.restTemplate;
        }
        
        /**
         * get方式提交请求
         * @param url
         *         请求URL
         * @param clazz
         *         请求响应的类型
         * @param urlVariables
         *         URL变量列表
         * @return
         */
        public static <T> T getForObject(String url, Class<T> clazz, Map<String, Object> urlVariables) {
            return restClientUtils.restTemplate.getForObject(url, clazz, urlVariables);
        }
    
        /**
         * post方式提交请求并且携带header信息
         * @param url
         *         请求URL
         * @param headsMap
         *         携带的HTTP Header参数列表
         * @param body
         *         消息体
         * @return
         */
        public static String postForObject(String url, Map<String, String> headsMap, String body) {
            HttpHeaders headers = new HttpHeaders();
            for (String key : headsMap.keySet()) {
                headers.add(key, headsMap.get(key));
            }
            HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
            String result = restClientUtils.restTemplate.postForObject(url, httpEntity, String.class);
            return result;
        }
        
        /**
         * post方式提交请求并且携带header信息
         * @param url
         *         请求URL
         * @param headsMap
         *         携带的HTTP Header参数列表
         * @param body
         *         消息体
         * @return
         */
        public static String getForObject(String url, Map<String, String> headsMap, String body) {
            HttpHeaders headers = new HttpHeaders();
            for (String key : headsMap.keySet()) {
                headers.add(key, headsMap.get(key));
            }
            HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
            String result = restClientUtils.restTemplate.getForObject(url, String.class);
            return result;
        }
        
        /**
         * post方式提交请求
         * @param url
         *         请求URL
         * @param body
         *         消息体
         * @return
         */
        public static String postForString(String url, String body) {
            HttpHeaders headers = new HttpHeaders();
            //setAccessToken(headers,request);
            MediaType type = MediaType
                    .parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
            headers.add("accessType","1");
            
            HttpEntity<String> formEntity = new HttpEntity<String>(body, headers);
            String result = restClientUtils.restTemplate.postForObject(url, formEntity,
                    String.class);
            if (result == null)
                return null;
            return result;
        }
    
    }
  • 相关阅读:
    进程、线程、协程
    C++内存模型
    动态库dll与静态库lib
    virtual 虚函数表
    C++面试随笔
    alloc()、malloc()、calloc()、realloc()区别及用法
    C/C++ 面试题记录
    VC底层钩子程序在Win7/Vista下无效
    JMeter安装之后修成中文版
    明天开始 新的旅程
  • 原文地址:https://www.cnblogs.com/wangquanyi/p/11640824.html
Copyright © 2011-2022 走看看