• http-restTemplate方式调用


    RestTemplate请求封装工具类

    package com.zkml.logistics.web.utils;
    
    import com.zkml.common.util.MyUtil;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.http.*;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.Map;
    
    /**
     * RestTemplate请求封装工具类
    public class RestTemplateUtil {
    
        private static final Logger logger = LoggerFactory.getLogger(RestTemplateUtil.class);
    
        private static RestTemplate TEMPLATE = new RestTemplate();
    
        /**
         * post方式提交实体类
         *
         * @param url
         * @param paramsMap
         * @return
         */
        public static String postForEntity(String url, Map<String, Object> paramsMap) {
            String json = MyUtil.getJsonString(paramsMap);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> httpEntity = new HttpEntity<>(json, headers);
            ResponseEntity response = TEMPLATE.postForEntity(url, httpEntity, String.class);
            logger.info("RestTemplate result:" + response.getBody());
            return response.getBody().toString();
        }
    
        /**
         * 带mlyToken请求,支持get/post等
         *
         * @param url
         * @param method
         * @param paramsMap
         * @param mlyToken
         * @return
         */
        public static String exchangeWithToken(String url, HttpMethod method, Map<String, Object> paramsMap, String mlyToken) {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", mlyToken);
            ResponseEntity response;
            if(HttpMethod.POST == method){
                String json = MyUtil.getJsonString(paramsMap);
                headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
                HttpEntity httpEntity = new HttpEntity<>(json, headers);
                response = TEMPLATE.postForEntity(url, httpEntity, String.class);
            }else if (HttpMethod.PUT == method){
                String json = MyUtil.getJsonString(paramsMap);
                headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
                HttpEntity httpEntity = new HttpEntity<>(json, headers);
                response = TEMPLATE.exchange(url, method, httpEntity, String.class, paramsMap);
            }else{
                HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null, headers);
                response = TEMPLATE.exchange(url, method, httpEntity, String.class, paramsMap);
            }
            logger.info("RestTemplate请求result:" + response.getBody());
            return response.getBody().toString();
        }
    }
    
  • 相关阅读:
    關于招聘新人
    JS在线打字练习 PHP
    useragent 分析 PHP
    webSql工具 PHP
    《网站开发人员应该知道的61件事》[解读] PHP
    HTMLCSS速查 PHP
    Flash文字转图片 PHP
    Flash简易文件上传 PHP
    Google 字体 API PHP
    Google 二维条码 API PHP
  • 原文地址:https://www.cnblogs.com/fangh816/p/13295656.html
Copyright © 2020-2023  润新知