記錄我自己的工作 get 請求 ,攜帶 請求頭 header (token) url 根據參數 動態拼接 參數 放入 map 動態拼接 工作疑問: 可以用更簡潔的方法 拼接 url 嗎 個人qq : 332893400 AngDH.Lee ...
記錄我自己的工作
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