RestTemplate實現(xiàn)發(fā)送帶headers的GET請求
RestTemplate 發(fā)送帶headers的GET請求
需求:發(fā)送自定義header的GET請求,header中需要插入一個簽名。
發(fā)送自定義header的POST請求
之前寫過一個類似的請求,但是是POST的。這個也摸了一段時間,自己看參數(shù)整了出來。代碼如下:
// header填充 LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); headers.put("Content-Type", Collections.singletonList("application/json;charset=UTF-8")); headers.put("signature", Collections.singletonList(makeSignature(form.getNewMobile()))); // body填充 JSONObject json = new JSONObject(); json.put("oldMobile", mobile); json.put("newMobile", form.getNewMobile()); HttpEntity<String> request = new HttpEntity<String>(json.toString(), headers); // 一個單例的restTemplate RestTemplate restTemplate = HttpInvoker.getRestTemplate(); // 發(fā)送請求 ResponseEntity<String> response = restTemplate.postForEntity(whiteListURL, request, String.class);
很簡單的想著,只需要把上面的postForEntity()改成get的就行,但不是這樣的。
發(fā)送自定義header的GET請求
Update: 2019/12/11
從鏈接學(xué)到了一種比較友好的寫法:
private static void getEmployees(){ final String uri = "http://localhost:8080/springrestexample/employees"; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); HttpEntity<String> entity = new HttpEntity<String>("parameters", headers); ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class); System.out.println(result); }
粗略看了看postForEntity()和getForEntity()這兩個方法的實現(xiàn),都是準(zhǔn)備參數(shù),然后調(diào)用execute()方法,如下:
// POST @Override public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException { RequestCallback requestCallback = httpEntityCallback(request, responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType); return nonNull(execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables)); } // GET @Override @Nullable public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException { RequestCallback requestCallback = acceptHeaderRequestCallback(responseType); HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger); return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables); }
區(qū)別就在于RequestCallback實例化的時候,傳的參數(shù)不一樣。POST的時候,是將header做為參數(shù)傳給了RequestCallback。再然后就是execute()中的GET和POST參數(shù)不一樣。到這個時候,發(fā)送自定義header的GET請求,已經(jīng)很明顯了。
實例化的函數(shù),都是public的。
如果不是public的,或者說我們不能直接訪問到,還可以考慮通過反射的方式去調(diào)用相關(guān)的方法,但這里不需要用反射了。
結(jié)果
// header填充 LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); headers.put("Content-Type", Collections.singletonList("application/json;charset=UTF-8")); headers.put("signature", Collections.singletonList(makeSignature(mobile))); // 獲取單例RestTemplate RestTemplate restTemplate = HttpInvoker.getRestTemplate(); HttpEntity request = new HttpEntity(headers); // 構(gòu)造execute()執(zhí)行所需要的參數(shù)。 RequestCallback requestCallback = restTemplate.httpEntityCallback(request, JSONObject.class); ResponseExtractor<ResponseEntity<JSONObject>> responseExtractor = restTemplate.responseEntityExtractor(JSONObject.class); // 執(zhí)行execute(),發(fā)送請求 ResponseEntity<JSONObject> response = restTemplate.execute(apiAddress + "/xxx/whitelist/check?phone=" + mobile, HttpMethod.GET, requestCallback, responseExtractor);
雖然很簡單,但是看似不可能,自己卻做到了、完成了,就很有成就感。
RestTemplate優(yōu)雅的發(fā)送Get請求
在我們的項目中,如果借助RestTemplate發(fā)送帶參數(shù)的Get請求,我們可以通過拼接字符串的方式將url拼接出來,比如下面這種方式:
String url = "http://127.0.0.1:8080/rest/get?name="+ name +"&id=" + id; ResponseEntity<RestVO> forEntity = restTemplate.getForEntity(url, RestVO.class);
然而這種方式不太優(yōu)雅,我們還可以通過以下幾種方式發(fā)送Get請求
方式1:使用占位符
String url = "http://127.0.0.1:8080/rest/path/{name}/{id}"; Map<String, Object> params = new HashMap<>(); params.put("name", "這是name"); params.put("id", 1L); ResponseEntity<RestVO> forEntity = restTemplate.getForEntity(url, RestVO.class, params);
Map的key要和url中的占位符一致
方式2:使用LinkedMultiValueMap和UriComponentsBuilder
String url = "http://127.0.0.1:8080/rest/get"; MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("name", "這是name"); params.add("id", "1"); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url); URI uri = builder.queryParams(params).build().encode().toUri(); ResponseEntity<RestVO> forEntity = restTemplate.getForEntity(uri, RestVO.class); return forEntity.getBody();
方式2看起來是最優(yōu)雅的,將參數(shù)的設(shè)置和url分離。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實現(xiàn)返回值數(shù)據(jù)脫敏的步驟詳解
這篇文章主要給大家介紹一下SpringBoot實現(xiàn)返回值數(shù)據(jù)脫敏的步驟,文章通過代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-07-07IDEA中@Autowired自動注入MyBatis?Mapper報紅警告的幾種解決方法
這篇文章主要介紹了IDEA中@Autowired自動注入MyBatis?Mapper報紅警告的幾種解決方法2024-02-02MyBatis中的關(guān)聯(lián)關(guān)系配置與多表查詢的操作代碼
本文介紹了在MyBatis中配置和使用一對多和多對多關(guān)系的方法,通過合理的實體類設(shè)計、Mapper接口和XML文件的配置,我們可以方便地進行多表查詢,并豐富了應(yīng)用程序的功能和靈活性,需要的朋友可以參考下2023-09-09Java案例實現(xiàn)不重復(fù)的隨機數(shù)
這篇文章主要介紹了Java案例實現(xiàn)不重復(fù)的隨機數(shù),通過創(chuàng)建Set集合對象,可以使用HashSet也可以使用TreeSet,區(qū)別在于TreeSet是排序后的,創(chuàng)建隨機數(shù)對象,獲取一個隨機數(shù)去重等操作,需要的朋友可以參考一下2022-04-04Spring Bean生命周期之Bean元信息的配置與解析階段詳解
這篇文章主要為大家詳細介紹了Spring Bean生命周期之Bean元信息的配置與解析階段,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03