zoukankan      html  css  js  c++  java
  • RestTemplate GET 请求添加header头和 多个参数的写法

     1     public String takePersonAccessRecord() {
     2        String url = "";
     3         HttpHeaders headers = new HttpHeaders();
     4         headers.add("Authorization", TOKEN);
     5         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
     6         HashMap<String, Object> map = new HashMap<>();
     7         map.put("pageNo", 1);
     8         map.put("pageSize", 5);
     9         if (!map.isEmpty()) {
    10             for (Map.Entry<String, Object> e : map.entrySet()) {
    11                 builder.queryParam(e.getKey(), e.getValue());
    12             }
    13             String realUrl = builder.build().toString(); // 在此处拼接真实请求地址  "?pageNo=1&pageSize=5"
    14             log.info("realUrl:{}", realUrl);
    15             HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers); 
    16             ResponseEntity<String> exchange = restTemplate.exchange(realUrl, HttpMethod.GET, httpEntity, String.class, map);
    17             log.info("返回结果:{}", exchange.getBody());
    18             if (StringUtils.isNotEmpty(exchange.getBody())) {
    19                 String body = exchange.getBody();
    20                 JSONObject jsonObject = JSONObject.parseObject(body);
    21                 String data = String.valueOf(jsonObject.get("data"));
    22                 log.info("data={}", data);
    23                 AccessRecordObject recordObject = JSON.parseObject(data, AccessRecordObject.class);
    24                 log.info("accessRecords={}", JSON.toJSONString(recordObject));
    25                 if (!recordObject.getList().isEmpty()) {
    26                     List<AccessRecord> list = recordObject.getList();
    27                     for (AccessRecord accessRecord : list) {
    28                         accessRecordMapper.insert(accessRecord);
    29                     }
    30                 }
    31             }
    32             return exchange.getBody();
    33         }
    34         return null;
    35     }
  • 相关阅读:
    SQLyog连接mysql8,报错1251
    Oracle日期函数
    git 回退
    git新建分支并指定拉去远程分支
    git创建分支并拉去远端分支代码
    git创建空白分支
    Maven 本地仓库明明有jar包,pom文件还是报错解决办法(Missing artifact...jar)
    SqlHelper类
    ADO.NET中的模型及对象
    MVC过滤器---异常处理过滤器
  • 原文地址:https://www.cnblogs.com/padazala/p/15324230.html
Copyright © 2011-2022 走看看