16.4.2 GET 资源

你可能意识到在表 16.2 中列出了两种执行 GET 请求的方法:getForObject() 和 getForEntity()。正如之前所描述的,每个方法又有三种形式的重载。三个 getForObject() 方法的签名如下:

<T> T getForObject(URI url, Class<T> responseType) throws RestClientException;

<T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException;

<T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;

类似地,getForEntity() 方法的签名如下:

<T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException;

<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException;

<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;

除了返回类型,getForEntity() 方法就是 getForObject() 方法的镜像。实际上,它们的工作方式大同小异。它们都执行根据 URL 检索资源的 GET 请求。它们都将资源根据 responseType 参数匹配为一定的类型。唯一的区别在于 getForObject() 只返回所请求类型的对象,而 getForEntity() 方法会返回请求的对象以及响应相关的额外信息。

让我们首先看一下稍微简单的 getForObject() 方法。然后再看看如何使用 getForEntity() 方法来从 GET 响应中获取更多的信息。

Last updated