Spring本身提供了訪問RESTFul服務的的工具RestTemplate,在Spring Android項目中也提供了該工具。正如其名所示,其概念上和JdbcTemplate, JmsTemplate類似,雖然RestTemplate看似主要在於和遵循RESTful架構的服務交互,但是其實RestTemplate可以作爲一個通用的http訪問工具使用。
RestTemplate 的底層默認使用java.net實現http處理,但是通過指定ClientHttpRequestFactory的不同實現其也可以配置爲使用 apache httpclient,HttpComponentsClientHttpRequestFactory作爲ClientHttpRequestFactory的實現則會用httpclient創建請求。
RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
RestTemplate 針對常用的六種http方法提供了相應的實現,每種方法都有三種重載,兩個以String方式接受請求地址的,該地址將在內部被編碼一次,所以傳入的地址需要是沒被編碼的,地址可以包括模板變量,這兩個方法的模板變量一個以Map<String, String>輸入,一個以可變String參數輸入。
String result = restTemplate.getForObject(
"http://example.com/hotels/{hotel}/bookings/{booking}", String.class,"42", "21");
Map<String, String> vars = Collections.singletonMap("hotel", "42");
String result = restTemplate.getForObject(
"http://example.com/hotels/{hotel}/rooms/{hotel}", String.class, vars);
剩下的一個方法以java.net.URI方式表示請求地址,Spring提供了UriComponentsBuilder可以簡化Uri對象的創建。
UriComponents uriComponents = UriComponentsBuilder.fromUriString(
"http://example.com/hotels/{hotel}/bookings/{booking}").build()
.expand("42", "21")
.encode();
URI uri = uriComponents.toUri();
獨立指定URI的每個部分
UriComponents uriComponents = UriComponentsBuilder.newInstance()
.scheme("http").host("example.com").path("/hotels/{hotel}/bookings/{booking}").build()
.expand("42", "21")
.encode();
URI uri = uriComponents.toUri();
除去一般的http方法有對應的實現外,exchange和execute方法則可作爲通用化的請求方法被使用,通過其只要底層的http實現支持,可以實現額外的http請求方法,比如PATCH。
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, String... urlVariables)
public interface RequestCallback {
void doWithRequest(ClientHttpRequest request) throws IOException;
}
RequestCallback 提供了一個使用execute時修改request head和body的機會。
exchange方法則是基於HttpEntity來表示請求,響應的。
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("MyRequestHeader", "MyValue");
HttpEntity requestEntity = new HttpEntity(requestHeaders);
HttpEntity response = template.exchange(
"http://example.com/hotels/{hotel}",
HttpMethod.GET, requestEntity, String.class, "42");
String responseHeader = response.getHeaders().getFirst("MyResponseHeader");
String body = response.getBody();
對於Http與對象轉換的支持,RestTemplate是通過HttpMessageConverter實現的,其在Spring MVC中也被廣泛使用。
public interface HttpMessageConverter {
// Indicate whether the given class and media type can be read by this converter.
boolean canRead(Class clazz, MediaType mediaType);
// Indicate whether the given class and media type can be written by this converter.
boolean canWrite(Class clazz, MediaType mediaType);
// Return the list of MediaType objects supported by this converter.
List getSupportedMediaTypes();
// Read an object of the given type from the given input message, and returns it.
T read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;
// Write an given object to the given output message.
void write(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;
}
默認情況下通過無參構造器創建RestTemplate時,已經有一些HttpMessageConverter
被默認配置:
- StringHttpMessageConverter
- FormHttpMessageConverter
- ByteArrayHttpMessageConverter
- MarshallingHttpMessageConverter
- MappingJackson2HttpMessageConverter
- MappingJackson2XmlHttpMessageConverter
- SourceHttpMessageConverter
- BufferedImageHttpMessageConverter
根據名字即可大概猜出其作用,需要注意的是FormHttpMessageConverter
在實現數據轉換時,對於MIME
爲application/x-www-form-urlencoded
的POST請求,表單數據是作爲MultiValueMap<String, String>
讀寫的。