RestTemplate详解

拼搏现实的明天。 2022-05-22 09:05 346阅读 0赞

概述

  1. RestTemplatehttpClient类似,都是java中可以模拟http请求的封装。httpClient的使用,已经在另一篇文章中有所论述,但是RestTemplatehttpClient更优雅,它是spring中的一个封装功能。
  2. RestTemplate也是java中的模板类。采用的设计模式中的模板模式。

源码解析

  1. RestTemplate继承InterceptingHttpAccessor抽象类,实现RestOperation接口
  2. public class RestTemplate extends InterceptingHttpAccessor implements RestOperations
  3. RestOperations定义了基本的Rest操作集合。RestTemplate实现了这个接口
  4. public interface RestOperations {
  5. // GET
  6. <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException;
  7. <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;
  8. <T> T getForObject(URI url, Class<T> responseType) throws RestClientException;
  9. <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException;
  10. <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;
  11. <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException;
  12. // HEAD
  13. HttpHeaders headForHeaders(String url, Object... uriVariables) throws RestClientException;
  14. HttpHeaders headForHeaders(String url, Map<String, ?> uriVariables) throws RestClientException;
  15. HttpHeaders headForHeaders(URI url) throws RestClientException;
  16. // POST
  17. URI postForLocation(String url, Object request, Object... uriVariables) throws RestClientException;
  18. URI postForLocation(String url, Object request, Map<String, ?> uriVariables) throws RestClientException;
  19. URI postForLocation(URI url, Object request) throws RestClientException;
  20. <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
  21. throws RestClientException;
  22. <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
  23. throws RestClientException;
  24. <T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException;
  25. <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
  26. throws RestClientException;
  27. <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
  28. throws RestClientException;
  29. <T> ResponseEntity<T> postForEntity(URI url, Object request, Class<T> responseType) throws RestClientException;
  30. // PUT
  31. void put(String url, Object request, Object... uriVariables) throws RestClientException;
  32. void put(String url, Object request, Map<String, ?> uriVariables) throws RestClientException;
  33. void put(URI url, Object request) throws RestClientException;
  34. // PATCH
  35. <T> T patchForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
  36. throws RestClientException;
  37. <T> T patchForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
  38. throws RestClientException;
  39. <T> T patchForObject(URI url, Object request, Class<T> responseType) throws RestClientException;
  40. // DELETE
  41. void delete(String url, Object... uriVariables) throws RestClientException;
  42. void delete(String url, Map<String, ?> uriVariables) throws RestClientException;
  43. void delete(URI url) throws RestClientException;
  44. // OPTIONS
  45. Set<HttpMethod> optionsForAllow(String url, Object... uriVariables) throws RestClientException;
  46. Set<HttpMethod> optionsForAllow(String url, Map<String, ?> uriVariables) throws RestClientException;
  47. Set<HttpMethod> optionsForAllow(URI url) throws RestClientException;
  48. // exchange
  49. <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
  50. Class<T> responseType, Object... uriVariables) throws RestClientException;
  51. <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
  52. Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException;
  53. <T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity,
  54. Class<T> responseType) throws RestClientException;
  55. <T> ResponseEntity<T> exchange(String url,HttpMethod method, HttpEntity<?> requestEntity,
  56. ParameterizedTypeReference<T> responseType, Object... uriVariables) throws RestClientException;
  57. <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
  58. ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables) throws RestClientException;
  59. <T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity,
  60. ParameterizedTypeReference<T> responseType) throws RestClientException;
  61. <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, Class<T> responseType) throws RestClientException;
  62. <T> ResponseEntity<T> exchange(RequestEntity<?> requestEntity, ParameterizedTypeReference<T> responseType)
  63. throws RestClientException;
  64. // general execution
  65. <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
  66. ResponseExtractor<T> responseExtractor, Object... uriVariables) throws RestClientException;
  67. <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
  68. ResponseExtractor<T> responseExtractor, Map<String, ?> uriVariables) throws RestClientException;
  69. <T> T execute(URI url, HttpMethod method, RequestCallback requestCallback,
  70. ResponseExtractor<T> responseExtractor) throws RestClientException;
  71. }
  72. PostForObjectPostForEntity的区别在于前者请求返回就是响应体,后者返回的是响应行,响应头,响应体。
  73. 这里也有一个基础的http访问类HttpAccessor,里面主要是一个工厂方法,用来构造HttpRequestFactory
  74. public abstract class HttpAccessor {
  75. protected final Log logger = LogFactory.getLog(getClass());
  76. private ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
  77. public void setRequestFactory(ClientHttpRequestFactory requestFactory) {
  78. Assert.notNull(requestFactory, "ClientHttpRequestFactory must not be null");
  79. this.requestFactory = requestFactory;
  80. }
  81. public ClientHttpRequestFactory getRequestFactory() {
  82. return this.requestFactory;
  83. }
  84. protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
  85. ClientHttpRequest request = getRequestFactory().createRequest(url, method);
  86. if (logger.isDebugEnabled()) {
  87. logger.debug("Created " + method.name() + " request for \"" + url + "\"");
  88. }
  89. return request;
  90. }
  91. }

springboot对RestTemplate的集成

  1. 首先添加配置;
  2. @Configuration
  3. public class RestTemplateConfig {
  4. @Bean
  5. public RestTemplate restTemplate(){
  6. RestTemplate restTemplate = new RestTemplate();
  7. restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
  8. return restTemplate;
  9. }
  10. }
  11. 不然在service中的autowired会注入不进去
  12. 再设置请求头
  13. HttpHeaders headers = new HttpHeaders();
  14. headers.setContentType(MediaType.APPLICATION_JSON);
  15. 请求行,请求头,请求体分装成 一个Entity
  16. HttpEntity<String> entity = new HttpEntity<>(JSONObject.toJSONString(dto),headers );
  17. 接下来就调用出
  18. ResponseEntity<String> r = restTemplate.exchange(ADD_URL, HttpMethod.POST, entity, String.class);
  19. 那就可以请求到结果了。

总结

  1. 整个restTemplatehttpClient使用更加优雅,分装的更好
  2. restTemplateorg.springframework.web.client包中的类,是spring提供的类。
  3. httpclientorg.apache.http.client包中的类,是apache提供的类。

发表评论

表情:
评论列表 (有 0 条评论,346人围观)

还没有评论,来说两句吧...

相关阅读

    相关 RestTemplate使用详解

    在开发中有时候经常需要一些Http请求,请求数据,下载内容,也有一些简单的分布式应用直接使用Http请求作为跨应用的交互协议。 在Java中有不同的Http请求方式,主要就是

    相关 详解 RestTemplate 操作

    详解 RestTemplate 操作 作为开发人员,我们经常关注于构建伟大的软件来解决业务问题。数据只是软件完成工作时 要处理的原材料。但是如果你问一下业务人员,数据

    相关 详解 RestTemplate 操作

    详解 RestTemplate 操作 作为开发人员,我们经常关注于构建伟大的软件来解决业务问题。数据只是软件完成工作时  要处理的原材料。但是如果你问一下业务人员,数