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 }