zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    
  • 相关阅读:
    MySQL数据库命令行界面不支持中文
    mysqldump使用方法(MySQL数据库的备份与恢复)
    MySQL性能测试初试(1)--sysbench
    composer安装
    Java关键字[static].md
    Docker容器
    Docker概述及安装
    Docker镜像
    定时任务[crontab]
    Linux下的curl工具
  • 原文地址:https://www.cnblogs.com/fangh816/p/13295656.html
Copyright © 2011-2022 走看看