用RestTemplate调取接口,取得返回数据,携带header,动态拼接url ,动态参数

记录我自己的工作

get 请求  ,携带 请求头 header (token)  

url 根据参数 动态拼接

参数   放入 map  动态拼接

private String lclUrl = "http://xxx.xxxx.com/lcl";

  

private String TOKEN360FOB_URL = "http://xxx.xxxxxx.com/token?username={name}&password={password}";

  

1 public JSONObject get360fobToken() {
2         String json = restTemplate.getForObject(TOKEN360FOB_URL, String.class, username, password);
3         JSONObject json1 = JSONObject.fromObject(json);
4         return json1;
5     }

 

 1 /**
 2      * 拼箱
 3      *
 4      * @param departure
 5      * @param departureDate
 6      * @param destination
 7      * @param destCountry
 8      * @param lsps
 9      * @param sortBy
10      * @param pageNum
11      * @param pageSize
12      * @return
13      */
14     public JSONObject getLclFreight(String departure, String departureDate, String destination, String destCountry, String lsps, String sortBy, int pageNum, int pageSize) {
15         return this.getFclAndLcl(departure, departureDate, destination,
16                 destCountry, lsps, sortBy, pageNum, pageSize, lclUrl);
17     }

 

 1 /**
 2      * 共同代码抽取
 3      *
 4      * @param departure
 5      * @param departureDate
 6      * @param destination
 7      * @param destCountry
 8      * @param lsps
 9      * @param sortBy
10      * @param pageNum
11      * @param pageSize
12      * @param xxxUrl
13      * @return
14      */
15     private JSONObject getFclAndLcl(String departure, String departureDate, String destination,
16                                     String destCountry, String lsps, String sortBy, int pageNum, int pageSize, String xxxUrl) {
17         JSONObject json = this.get360fobToken();
18         String t = json.getString("content");
19         String token = "Bearer " + t;
       // 将token 放入请求头 20 HttpHeaders requestHeaders = new HttpHeaders(); 21 requestHeaders.add("Authorization", token); 22 23 //参数 24 Map<String, Object> uriVariables = new HashMap<String, Object>(); 25 //拼接url 26 StringBuffer buffer = new StringBuffer(); 27 buffer.append("?"); 28 if (StringUtils.isNotBlank(departure)) { 29 buffer.append("departure={departure}").append("&"); 30 uriVariables.put("departure", departure); 31 } 32 if (StringUtils.isNotBlank(departureDate)) { 33 buffer.append("departureDate={departureDate}").append("&"); 34 uriVariables.put("departureDate", departureDate); 35 } 36 if (StringUtils.isNotBlank(destination)) { 37 buffer.append("destination={destination}").append("&"); 38 uriVariables.put("destination", destination); 39 } 40 if (StringUtils.isNotBlank(destCountry)) { 41 buffer.append("destCountry={destCountry}").append("&"); 42 uriVariables.put("destCountry", destCountry); 43 } 44 if (StringUtils.isNotBlank(lsps)) { 45 buffer.append("lsps={lsps}").append("&"); 46 uriVariables.put("lsps", lsps); 47 } 48 if (StringUtils.isNotBlank(sortBy)) { 49 buffer.append("sortBy={sortBy}").append("&"); 50 uriVariables.put("sortBy", sortBy); 51 } 52 53 buffer.append("pageNum={pageNum}").append("&"); 54 buffer.append("pageSize={pageSize}"); 55 uriVariables.put("pageNum", pageNum); 56 uriVariables.put("pageSize", pageSize); 57 String url = xxxUrl + buffer.toString(); 58 59 HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders); 60 61 ResponseEntity<String> response = 62 restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class, uriVariables); 63 String resBody = response.getBody(); 64 JSONObject temp = JSONObject.fromObject(resBody); 65 66 67 return temp; 68 }

工作疑问:

可以用更简洁的方法 拼接 url 吗

个人qq : 332893400

AngDH.Lee

上一篇:RestTemplate优雅的发送Get请求


下一篇:RestTemplate控制台打印请求响应日志