zoukankan      html  css  js  c++  java
  • spingBoot RestTemplate get和post请求封装

    public ResponseEntity<String> sendParamsMapForResponseEntity(String url, HttpMethod httpMethod,
                Map<String, String> headersMap, Map<String, Object> paramsMap) {
            HttpHeaders httpHeaders = null;
            if (MapUtils.isNotEmpty(headersMap)) {
                httpHeaders = new HttpHeaders();
                for (Map.Entry<String, String> entry : headersMap.entrySet()) {
                    httpHeaders.add(entry.getKey(), entry.getValue());
                }
            }
    
            StringBuffer urlStringBuffer = new StringBuffer(url);
    
            HttpEntity<Object> httpEntity = null;
            if (HttpMethod.GET.equals(httpMethod)) {
                if (MapUtils.isNotEmpty(paramsMap)) {
                    urlStringBuffer.append("?");
                    int index = 0;
                    for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
                        if (index == 0) {
                            urlStringBuffer.append(entry.getKey()).append("=").append(entry.getValue());
                        } else {
                            urlStringBuffer.append("&").append(entry.getKey()).append("=").append(entry.getValue());
                        }
                        index++;
                    }
                }
                httpEntity = new HttpEntity<Object>(httpHeaders);
            } else {
                if (MapUtils.isNotEmpty(headersMap)
                        && MediaType.APPLICATION_JSON_VALUE.equals(headersMap.get(HttpHeaders.CONTENT_TYPE))) {
                    httpEntity = new HttpEntity<Object>(JSON.toJSONString(paramsMap), httpHeaders);
                } else {
                    httpEntity = new HttpEntity<Object>(paramsMap, httpHeaders);
                }
            }
            ResponseEntity<String> responseEntity = baseRestTemplate.exchange(urlStringBuffer.toString(), httpMethod,
                    httpEntity, String.class);
    
            return responseEntity;
        }

    spingBoot RestTemplate get和post请求封装

  • 相关阅读:
    doges
    Unity Fps示例
    使用Unity的2D功能开发弹球游戏
    Unity UGUI 原理篇(二):Canvas Scaler 縮放核心
    UGUI 深度優化提升手遊效能
    关于Unity中的UGUI优化,你可能遇到这些问题
    git branch --set-upstream 本地关联远程分支
    git rm 与 git reset
    Git笔记之初识vi编辑器
    [内容分享]粗略判断Shader每条代码的成本
  • 原文地址:https://www.cnblogs.com/zxf330301/p/13748462.html
Copyright © 2011-2022 走看看