1. 程式人生 > >Spring RestTemplate中get,post,put,delete請求方式

Spring RestTemplate中get,post,put,delete請求方式

GET請求

在RestTemplate中,傳送一個GET請求,我們可以通過如下兩種方式:

第一種:getForEntity

getForEntity方法的返回值是一個ResponseEntity<T>ResponseEntity<T>是Spring對HTTP請求響應的封裝,包括了幾個重要的元素,如響應碼、contentType、contentLength、響應訊息體等。比如下面一個例子:

@RequestMapping("/gethello")
public String getHello() {
    ResponseEntity<String> responseEntity = restTemplate.getForEntity
("http://HELLO-SERVICE/hello", String.class); String body = responseEntity.getBody(); HttpStatus statusCode = responseEntity.getStatusCode(); int statusCodeValue = responseEntity.getStatusCodeValue(); HttpHeaders headers = responseEntity.getHeaders(); StringBuffer result = new StringBuffer();
result.append("responseEntity.getBody():").append(body).append("<hr>") .append("responseEntity.getStatusCode():").append(statusCode).append("<hr>") .append("responseEntity.getStatusCodeValue():").append(statusCodeValue).append("<hr>") .append
("responseEntity.getHeaders():").append(headers).append("<hr>"); return result.toString(); }

關於這段程式碼,我說如下幾點:

  • getForEntity的第一個引數為我要呼叫的服務的地址,這裡我呼叫了服務提供者提供的/hello介面,注意這裡是通過服務名呼叫而不是服務地址,如果寫成服務地址就沒法實現客戶端負載均衡了。
  • getForEntity第二個引數String.class表示我希望返回的body型別是String
  • 拿到返回結果之後,將返回結果遍歷打印出來

最終顯示結果如下:

responseEntity.getBody():hello

responseEntity.getStatusCode():200

responseEntity.getStatusCodeValue():200

responseEntity.getHeaders():{Content-Type=[text/plain;charset=UTF-8],Content-Length=[5],Date=[2018 10:00:00 GMT]}

有時候我在呼叫服務提供者提供的介面時,可能需要傳遞引數,有兩種不同的方式,如下:

@RequestMapping("/sayhello")
public String sayHello() {
    ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={1}", String.class, "張三");
    return responseEntity.getBody();
}
@RequestMapping("/sayhello2")
public String sayHello2() {
    Map<String, String> map = new HashMap<>();
    map.put("name", "李四");
    ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={name}", String.class, map);
    return responseEntity.getBody();
}
  • 可以用一個數字做佔位符,最後是一個可變長度的引數,來一一替換前面的佔位符
  • 也可以前面使用name={name}這種形式,最後一個引數是一個map,map的key即為前邊佔位符的名字,map的value為引數值

第一個呼叫地址也可以是一個URI而不是字串,這個時候我們構建一個URI即可,引數神馬的都包含在URI中了,如下:

@RequestMapping("/sayhello3")
public String sayHello3() {
    UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://HELLO-SERVICE/sayhello?name={name}").build().expand("王五").encode();
    URI uri = uriComponents.toUri();
    ResponseEntity<String> responseEntity = restTemplate.getForEntity(uri, String.class);
    return responseEntity.getBody();
}

通過Spring中提供的UriComponents來構建Uri即可。

當然,服務提供者不僅可以返回String,也可以返回一個自定義型別的物件,比如我的服務提供者中有如下方法:

@RequestMapping(value = "/getbook1", method = RequestMethod.GET)
public Book book1() {
    return new Book("三國演義", 90, "羅貫中", "花城出版社");
}

對於該方法我可以在服務消費者中通過如下方式來呼叫:

@RequestMapping("/book1")
public Book book1() {
    ResponseEntity<Book> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/getbook1", Book.class);
    return responseEntity.getBody();
}

執行結果如下:

{"name:""三國演義","price":"90","author":"羅貫中","publisher":"花城出版社"}

第二種:getForObject

getForObject函式實際上是對getForEntity函式的進一步封裝,如果你只關注返回的訊息體的內容,對其他資訊都不關注,此時可以使用getForObject,舉一個簡單的例子,如下:

@RequestMapping("/book2")
public Book book2() {
    Book book = restTemplate.getForObject("http://HELLO-SERVICE/getbook1", Book.class);
    return book;
}

getForObject也有幾個過載方法,如下:


這幾個過載方法引數的含義和getForEntity一致,我就不再贅述了。

POST請求

在RestTemplate中,POST請求可以通過如下三個方法來發起

第一種:postForEntity

該方法和get請求中的getForEntity方法類似,如下例子:

@RequestMapping("/book3")
public Book book3() {
    Book book = new Book();
    book.setName("紅樓夢");
    ResponseEntity<Book> responseEntity = restTemplate.postForEntity("http://HELLO-SERVICE/getbook2", book, Book.class);
    return responseEntity.getBody();
}
  • 方法的第一引數表示要呼叫的服務的地址
  • 方法的第二個引數表示上傳的引數
  • 方法的第三個引數表示返回的訊息體的資料型別

我這裡建立了一個Book物件,這個Book物件只有name屬性有值,將之傳遞到服務提供者那裡去,服務提供者程式碼如下:

@RequestMapping(value = "/getbook2", method = RequestMethod.POST)
public Book book2(@RequestBody Book book) {
    System.out.println(book.getName());
    book.setPrice(33);
    book.setAuthor("曹雪芹");
    book.setPublisher("人民文學出版社");
    return book;
}

服務提供者接收到服務消費者傳來的引數book,給其他屬性設定上值再返回,呼叫結果如下:

{"name:""紅樓夢","price":"33","author":"曹雪芹","publisher":"人民文學出版社"}

postForEntity的其他過載方法如下:


這些方法的引數含義和getForEntity引數的含義一致,不再贅述。

第二種:postForObject

如果你只關注,返回的訊息體,可以直接使用postForObject。用法和getForObject一致。

第三種:postForLocation

postForLocation也是提交新資源,提交成功之後,返回新資源的URI,postForLocation的引數和前面兩種的引數基本一致,只不過該方法的返回值為Uri,這個只需要服務提供者返回一個Uri即可,該Uri表示新資源的位置。

PUT請求

在RestTemplate中,PUT請求可以通過put方法呼叫,put方法的引數和前面介紹的postForEntity方法的引數基本一致,只是put方法沒有返回值而已。舉一個簡單的例子,如下:

@RequestMapping("/put")
public void put() {
    Book book = new Book();
    book.setName("紅樓夢");
    restTemplate.put("http://HELLO-SERVICE/getbook3/{1}", book, 99);
}

book物件是我要提交的引數,最後的99用來替換前面的佔位符{1}

DELETE請求

delete請求我們可以通過delete方法呼叫來實現,如下例子:

@RequestMapping("/delete")
public void delete() {
    restTemplate.delete("http://HELLO-SERVICE/getbook4/{1}", 100);
}

delete方法也有幾個過載的方法,不過過載的引數和前面基本一致,不贅述。