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;
        }
    
    }
  • 相关阅读:
    sencha touch 扩展篇之将sencha touch打包成安装程序(上)- 使用sencha cmd打包安装程序
    sencha touch 扩展篇之使用sass自定义主题样式 (下)通过css修改官方组件样式以及自定义图标
    一个不错的android组件的网站
    sencha touch 扩展篇之使用sass自定义主题样式 (上)使用官方的api修改主题样式
    sencha touch 入门系列 (九) sencha touch 布局layout
    面试题总结
    国外接活网站Elance, Freelancer和ScriptLance的介绍和对比
    sencha touch 入门系列 扩展篇之sencha touch 项目打包压缩
    Android Design Support Library——Navigation View
    设计模式——命令模式
  • 原文地址:https://www.cnblogs.com/wangquanyi/p/11640824.html
Copyright © 2011-2022 走看看