Synchronous client to perform HTTP requests, exposing a simple, template method API over underlying HTTP client libraries such as the JDK HttpURLConnection, Apache HttpComponents, and others.
The RestTemplate offers templates for common scenarios by HTTP method, in addition to the generalized exchange and execute methods that support of less frequent cases.
1. RestTemplate概述
最近需要在Springboot项目中添加post功能并且通过api下载文件到本地。在这个过程中,如果直接使用HttpClient可能会有点繁琐,而RestTemplate就很好的解决了这个问题。RestTemplate 采用同步方式执行 HTTP 请求的类,底层使用 JDK 原生 HttpURLConnection API ,或者 HttpComponents等其他 HTTP 客户端请求类库。
更重要的是,通过它我们可以很容易地发送http请求。
因为网上很多使用RestTemplate的教程,所以以下小结主要是分享我使用过程中遇到的几个问题以及解决方案。
2. RestTemplate配置@Retry
- pom.xml依赖。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId

本文介绍如何使用Spring的RestTemplate进行HTTP请求,并重点讲解了配置重试机制及消息转换器的方法。
最低0.47元/天 解锁文章
4879

被折叠的 条评论
为什么被折叠?



