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;
        }
    
    }
  • 相关阅读:
    SQL Server CHARINDEX和PATINDEX详解
    Delphi7控件包详解 (转)
    经历一次人际关系危机总结,改进
    2008年5月份目标和计划
    God bless 贝贝!
    开始新的生活~~~
    一切安好~~~
    Hadoop : “Moving Computation is Cheaper than Moving Data”
    改论文,不轻言放弃,力求准确,简洁
    杜绝拖拖拉拉《明日歌》
  • 原文地址:https://www.cnblogs.com/wangquanyi/p/11640824.html
Copyright © 2011-2022 走看看