zoukankan      html  css  js  c++  java
  • RestTemplate之post,get请求

    1.RestTemplate之post请求

    A.带header并传递给第三方数据格式如下

    {
        "version": "1.0.0",
        "token": "token",
        "sign": "sign"
    }

    如下组织数据与传递

    // header数据
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Basic xxx");
    headers.set("Content-Type", "application/json");
    headers.set("Accept", "application/json");
    
    // 组织要传递的数据
    Map<String,Object> requestBody = new HashedMap();
    requestBody.put("version", "1.0.0");
    requestBody.put("token", "token");
    requestBody.put("sign", "sign");
    
    String  requestUrl = "请求的URL";
    HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers); 
    RestTemplate restTemplate
    = new RestTemplate();
    ResponseEntity
    <String> response = restTemplate.postForEntity(requestUrl, requestEntity, String.class);

    接收方收到数据格式如下

    {
        "token": "token",
        "sign": "sign",
        "version": "1.0.0"
    }

    B.带header并传递给第三方数据格式如下

    {
        "action": "action",
        "body": {
            "version": "version",
            "sign": "sign",
            "content": {
                "id": "1",
                "name": "name"
            }
        }
    }

    如下组织与传递

    // header数据
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Basic xxx");
    headers.set("Content-Type", "application/json");
    headers.set("Accept", "application/json");
    
    // 组织数据
    Map<String, Object> paramMap = new HashedMap();
    Map<String,Object> subMap = new HashedMap();
    Map<String,String> bodyText = new HashedMap();
    
    bodyText.put("id","1");
    bodyText.put("name","name");
    
    subMap.put("version","version");
    subMap.put("sign","sign");
    subMap.put("content",bodyText);
    
    paramMap.put("action", "action");
    paramMap.put("body", subMap);
    
    String requestUrl = "请求的URL";
    
    HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(paramMap, headers);
    RestTemplate restTemplate = new RestTemplate(); 
    ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class, paramMap);

     接收方收到数据格式如下

    {
        "action": "action",
        "body": {
            "content": {
                "name": "name",
                "id": "1"
            },
            "sign": "sign",
            "version": "version"
        }
    }

     2.RestTemplate之get请求

    A.传递header并通过URL传递参数 a=AA&b=BB

    // header数据
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Basic xxx");
    headers.set("Content-Type", "application/json");
    headers.set("Accept", "application/json");
    
    String requestUrl = "请求的URL?a=AA&b=BB";
    Map<String,Object> paramMap = new HashedMap();
    HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(paramMap, headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.GET, httpEntity, String.class, paramMap);
  • 相关阅读:
    201-STM32+Air724UG基本控制篇(阿里云物联网平台)-设备使用物模型Topic上报温湿度数据
    Sentinel Go 核心统计结构滑动窗口的深度解析
    golang sync.Mutex互斥锁的实现原理
    Golang-Scheduler原理解析
    Golang-Channel原理解析
    golang里channel的实现原理
    最长回文子序列
    GO语言的goroutine并发原理和调度机制
    golang 常见问题
    通过js给网页加上水印背景
  • 原文地址:https://www.cnblogs.com/lin3615/p/13745990.html
Copyright © 2011-2022 走看看