RestTemplate访问RESTful服务

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在实现数据转换时,对于MIMEapplication/x-www-form-urlencoded的POST请求,表单数据是作为MultiValueMap<String, String>读写的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章