1. 程式人生 > >筆記:Spring Cloud Ribbon RestTemplate 詳解

筆記:Spring Cloud Ribbon RestTemplate 詳解

定義 方法 template 包含 mediatype pst ron request temp

詳細介紹RestTemplate 針對幾種不同請求類型和參數類型的服務調用實現,示例代碼中的 restTemplate 都是通過Spring 註入方式創建的,相關代碼如下:

@Autowired

private RestTemplate restTemplate;

? ?

在應用主類需要增加 Bean,代碼如下:

@LoadBalanced

@Bean

public RestTemplate createRestTemplate(){

????????return new RestTemplate();

}

  • GET 請求

    RestTemplate 中,對GET請求可以通過如下方法進行調用:

    • getForEntity 方法:該方法返回的是 ResponseEntity,該對象是Spring HTTP 請求響應對象的封裝,其中主要存儲了 HTTP 的幾個重要元素,比如 HTTP 請求狀態的枚舉對象 HTTPStatus 、在他的父類 HTTPEntity 中還存儲著 HTTP 的頭信息對象
      HTTPHeaders 以及泛型類型的請求體對象,例如,如下代碼訪問服務的 /get 請求,由於第二個參數為 String.class 因此返回的 ResponseEntity 對象中的 body 內容類型轉換為字符串返回:

    ???????????? ResponseEntity<String> responseEntity = null;

????????????????????????responseEntity =

????????????????????????????????????????restTemplate.getForEntity("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/get, String.class);

????????????????????????if (responseEntity.getStatusCode() == HttpStatus.OK) {

????????????????????????????????return responseEntity.getBody();

????????????????????????}

????????????????????????return "response status " + responseEntity.getStatusCodeValue();

  • getForObject 方法:該方法可以理解為對getForEntity 的進一步封裝,通過 HttpMessageConverterExtractor HTTP 請求響應體 body 聶榮進行對象轉換,實現請求直接返回包裝好的對象內容:

    ??String body =

    ??????????????????restTemplate.getForObject("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/get", String.class);

  • POST 請求

    在RestTemplate 中,對 POST 請求可以通過如下三個方法進行調用實現。

    • postForEntity 方法:該方法同GET請求中的 getForEntity 類似,會在調用後返回 ResponseEntity<T>對象,其中 T 為請求響應的 body 類型,示例代碼如下:

    ????????????User user = new User("didi",30);

    ????????????ResponseEntity<String> responseEntity =

    ???????????? restTemplate.postForEntity("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/post",user,String.class);

    ????????????String body = responseEntity.getBody();

    這裏需要註意的是新增加的 request 參數,該參數可以是一個普通對象,也可以是一個 HttpEntity 對象,如果是一個普通對象時,RestTemplate 會將請求對象轉換為一個 HttpEntity 對象來處理,並且把該對象視為完整的 body 來處理;如果是一個 HttpEntity 對象,那麽就會當作一個完整的HTTP 請求對象來處理,這個對象不僅包含了 body 內容,也包含了 header 內容,示例代碼如下:

    ResponseEntity<String> responseEntity = null;

    HttpHeaders headers = new HttpHeaders();

    List<MediaType> accept = new LinkedList<>();

    accept.add(MediaType.APPLICATION_JSON_UTF8);

    headers.setAccept(accept);

    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    ? ?

    MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<>();

    postParameters.add("id", "11");

    postParameters.add("name", "aoa");

    postParameters.add("comment", "");

    ? ?

    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(postParameters, headers);

    ? ?

    responseEntity =

    ??????????????restTemplate.postForEntity("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/webapi/hello/post", requestEntity, String.class);

    String body = responseEntity.getBody();

    ? ?

    • postForObject 方法:該方法跟 getForObject 的類型類似,他的做用是簡化 postForEntity 的後續處理,通過直接將請求響應的 body 內容包裝成對象來返回使用,示例代碼如下:

    ????????????User user = new User("didi",30);

    ????????????String body =

    ??????????? ? restTemplate..postForObject("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/post",user,String.class);

    • postForLocation 方法:該方法實現了以 POST 請求提交資源,並返回資源的 URI,該 URI 就相當於指定了返回類型,所以此方法實現的 POST 請求不需要像 postForEntity postForObject 那樣指定 responseType,示例代碼如下:

    ????????????User user = new User("didi",30);

    ????????????URI responseURI =

    restTemplate.postForLocation("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/post",user);

  • PUT 請求

    RestTemplate 中,對與 put 請求其返回為 void 類型,沒有返回內容,因此,就沒有其他函數定義的 responseType 參數,除此之外的其他傳入參數定義與用法與 postForObject 基本一致,示例代碼如下:

    Map<String, String> map = new HashMap<>();

    map.put("put1", "23434");

    map.put("put2", "3544545");

    ???????????????? ?

    restTemplate.put("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/webapi/hello/put/{0}/{1}", map, 123213, "桑德蘭副科級");

  • DELETE請求

    RestTemplate 中,對 DELETE 請求可以通過 delete 方法進行調用實現,和put請求一致,其返回類型為 void 類型,因此 DELETE 請求不需要返回數據,示例代碼如下:

    ?Integer id = 100;

    ?restTemplate.delete("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/delete?id={1}", id);

筆記:Spring Cloud Ribbon RestTemplate 詳解