1. 程式人生 > >Spring Boot 2關於http介面呼叫(3)

Spring Boot 2關於http介面呼叫(3)

使用RestTemplate

RestTemplate是從Spring 3 就有的功能,這裡主要簡單介紹幾個常用介面的使用

RestOperations中常用的介面

public class RestTemplate extends InterceptingHttpAccessor implements RestOperations

RestTemplate實現了RestOperations介面,裡面定義常用的各種方法

// GET請求返回實際物件
<T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;
// GET請求返回ResponseEntity,包含狀態碼實際物件
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;

// POST請求返回實際物件
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException;
// POST請求返回ResponseEntity,包含狀態碼實際物件
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException;

// 自定義請求方法與泛型返回型別
<T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType) throws RestClientException;
// 自定義請求方法與泛型返回型別
<T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables) throws RestClientException;

單元測試

@Test
public void test1() {
        long begin = System.currentTimeMillis();

        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("msg", "這是一條測試資料");
		// GET返回ResponseEntity
        ResponseEntity<String> responseEntity = new RestTemplate().getForEntity("http://localhost:9999/demo/hello?msg={msg}", String.class, paraMap);
        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getBody());

		// GET返回實際型別
        String rtn = new RestTemplate().getForObject("http://localhost:9999/demo/hello?msg={msg}", String.class, paraMap);
        System.out.println(rtn);

		// POST返回ResponseEntity
        responseEntity = new RestTemplate().postForEntity("http://localhost:9999/demo/hello", paraMap, String.class);
        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getBody());
		
		// POST返回實際型別
        rtn = new RestTemplate().postForObject("http://localhost:9999/demo/hello", paraMap, String.class);
        System.out.println(rtn);

        long end = System.currentTimeMillis();
        System.out.println("*****消耗時間(秒):" + (end - begin) / 1000.0);
}

@Test
public void test2() {
        long begin = System.currentTimeMillis();
		// 構造複雜的返回型別HttpRspDTO<String>
        ParameterizedTypeReference<HttpRspDTO<String>> parameterizedTypeReference = new ParameterizedTypeReference<HttpRspDTO<String>>() {
            @Override
            public Type getType() {
                return ParameterizedTypeImpl.make(HttpRspDTO.class, new Type[]{String.class}, null);
            }
        };

		// 不帶引數的GET請求
        ResponseEntity<HttpRspDTO<String>> responseEntity = new RestTemplate().exchange(URI.create("http://localhost:9999/demo/hello"), HttpMethod.GET, null, parameterizedTypeReference);

        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getBody());

		// 帶引數的GET請求
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("msg", "這是一條測試資料");
        responseEntity = new RestTemplate().exchange("http://localhost:9999/demo/hello?msg={msg}", HttpMethod.GET, null, parameterizedTypeReference, paraMap);

        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getBody());

        long end = System.currentTimeMillis();
        System.out.println("*****消耗時間(秒):" + (end - begin) / 1000.0);
}

原始碼位置