java中可以使用3種方式調用api
- HttpURLConnection
- HttpClient
- RestTemplate
這里要講的是RestTemplate的方式。
REST的基礎知識
當談論REST時,有一種常見的錯誤就是將其視為“基於URL的Web服務”——將REST作為另一
種類型的遠程過程調用(remote procedure call,RPC)機制,就像SOAP一樣,只不過是通過簡單
的HTTP URL來觸發,而不是使用SOAP大量的XML命名空間
恰好相反,REST與RPC幾乎沒有任何關系。RPC是面向服務的,並關注於行為和動作;而REST
是面向資源的,強調描述應用程序的事物和名詞。
更簡潔地講,REST就是將資源的狀態以最適合客戶端或服務端的形式從服務器端轉移到客戶
端(或者反過來)。
在REST中,資源通過URL進行識別和定位。至於RESTful URL的結構並沒有嚴格的規則,但是
URL應該能夠識別資源,而不是簡單的發一條命令到服務器上。再次強調,關注的核心是事
物,而不是行為.,
Spring 中如何使用Rest資源
借助 RestTemplate,Spring應用能夠方便地使用REST資源
Spring的 RestTemplate訪問使用了模版方法的設計模式.
模版方法將過程中與特定實現相關的部分委托給接口,而這個接口的不同實現定義了接口的不同行為.
RestTemplate定義了36個與REST資源交互的方法,其中的大多數都對應於HTTP的方法。
其實,這里面只有11個獨立的方法,其中有十個有三種重載形式,而第十一個則重載了六次,這樣一共形成了36個方法。
- delete() 在特定的URL上對資源執行HTTP DELETE操作
- exchange() 在URL上執行特定的HTTP方法,返回包含對象的ResponseEntity,這個對象是從響應體中映射得到的execute() 在URL上執行特定的HTTP方法,返回一個從響應體映射得到的對象
- getForEntity() 發送一個HTTP GET請求,返回的ResponseEntity包含了響應體所映射成的對象
- getForObject() 發送一個HTTP GET請求,返回的請求體將映射為一個對象
- postForEntity() POST 數據到一個URL,返回包含一個對象的ResponseEntity,這個對象是從響應體中映射得到的
- postForObject() POST 數據到一個URL,返回根據響應體匹配形成的對象
- headForHeaders() 發送HTTP HEAD請求,返回包含特定資源URL的HTTP頭
- optionsForAllow() 發送HTTP OPTIONS請求,返回對特定URL的Allow頭信息
- postForLocation() POST 數據到一個URL,返回新創建資源的URL
- put() PUT 資源到特定的URL
實際上,由於Post 操作的非冪等性,它幾乎可以代替其他的CRUD操作.
Get請求
RestTemplate 的get方法有以上幾個,可以分為兩類: getForEntity() 和 getForObject()
首先看 getForEntity() 的返回值類型 ResponseEntity
<T> ResponseEntity<T> getForEntity()
它繼承了HttpEntity. 封裝了返回的響應信息,包括 響應狀態,響應頭 和 響應體.
測試: getForEntity
- 無參數的 getForEntity 方法
@RequestMapping("getForEntity") public List<UserEntity> getAll2() { ResponseEntity<List> responseEntity = restTemplate.getForEntity("http://localhost/getAll", List.class); HttpHeaders headers = responseEntity.getHeaders(); HttpStatus statusCode = responseEntity.getStatusCode(); int code = statusCode.value(); List<UserEntity> list = responseEntity.getBody(); System.out.println(list.toString()); return list; }
2.有參數的 getForEntity 請求,參數列表,可以使用 {} 進行url路徑占位符
//有參數的 getForEntity 請求,參數列表 @RequestMapping("getForEntity/{id}") public UserEntity getById2(@PathVariable(name = "id") String id) { ResponseEntity<UserEntity> responseEntity = restTemplate.getForEntity("http://localhost/get/{id}", UserEntity.class, id); UserEntity userEntity = responseEntity.getBody(); return userEntity; }
3.有參數的 get 請求,使用map封裝參數
//有參數的 get 請求,使用map封裝參數 @RequestMapping("getForEntity/{id}") public UserEntity getById4(@PathVariable(name = "id") String id) { HashMap<String, String> map = new HashMap<>(); map.put("id",id); ResponseEntity<UserEntity> responseEntity = restTemplate.getForEntity("http://localhost/get/{id}", UserEntity.class, map); UserEntity userEntity = responseEntity.getBody(); return userEntity; }
這里也可以用JSONObject。
而對於上傳文件時,可以使用
MultiValueMap<String, Object> resultMap = new LinkedMultiValueMap<>(); Resource resource = new FileSystemResource(file); param.put("file", resource);
參考網址:
https://blog.csdn.net/itguangit/article/details/78825505
上傳文件
https://blog.csdn.net/weixin_30539625/article/details/102054205