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;
        }
    
    }
  • 相关阅读:
    [arXiv18]更快的基于非二叉化自底向上策略的转移系统成分句法分析
    [AAAI18]面向序列建模的元多任务学习
    [COLING18]两种成分句法分析的局部特征模型
    [ACL18]基于RNN和动态规划的线性时间成分句法分析
    成分句法分析综述
    [NAACL16]RNN文法
    [TACL17]基于中序转移的成分句法分析
    K-摇臂赌博机算法与实现
    关于JVM案例分析(四)
    关于JVM案例分析(三)
  • 原文地址:https://www.cnblogs.com/wangquanyi/p/11640824.html
Copyright © 2011-2022 走看看