Spring Boot 是一个基于 Java 的开源框架,用于简化 Spring 应用程序的创建和部署。在 Spring Boot 中,可以使用 RestTemplate 类来调用 Restful 接口。RestTemplate 是 Spring 提供的用于访问 RESTful 服务的客户端类,它支持同步的 HTTP 请求和响应。
下面是在 Spring Boot 中使用 RestTemplate 调用 Restful 接口的步骤:
- 添加依赖
首先,确保你的 Spring Boot 项目中已经添加了 RestTemplate 的依赖。在 Maven 项目中,可以在 pom.xml 文件中添加以下依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
对于 Gradle 项目,可以在 build.gradle 文件中添加以下依赖:implementation 'org.springframework.boot:spring-boot-starter-web'
- 配置 RestTemplate
在 Spring Boot 中,RestTemplate 的配置通常在 application.properties 或 application.yml 文件中完成。你可以配置 RestTemplate 的基本属性,例如连接超时时间、读取超时时间等。例如,在 application.properties 文件中添加以下配置:spring.http.client.connection-timeout=5000spring.http.client.read-timeout=10000
- 创建 RestTemplate Bean
在你的 Spring Boot 应用程序中,创建一个 RestTemplate Bean。这可以通过实现一个配置类并添加一个 @Bean 方法来完成。例如:import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;import org.springframework.web.client.RestTemplate;@Configurationpublic class RestTemplateConfig {@Beanpublic RestTemplate restTemplate() {HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();factory.setConnectionRequestTimeout(5000); // 设置连接超时时间(毫秒)factory.setConnectTimeout(5000); // 设置连接超时时间(毫秒)factory.setReadTimeout(10000); // 设置读取超时时间(毫秒)return new RestTemplate(factory);}}
- 发送 GET 请求
使用 RestTemplate 发送 GET 请求非常简单。只需调用 restTemplate().getForObject() 方法并传入 URL 和返回值类型即可。例如:
```java
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
import static org.junit.Assert.;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.; // for HttpStatus and MediaType classes import static org.springframework.http.*ResponseEntity; // for ResponseEntity class import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; // for MockMvcRequestBuilders class import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; // for MockMvcResultMatchers class import static org.hamcrest.Matchers.*; // for Matchers class @RunWith(SpringJUnit4ClassRunner.class); @SpringBootTest(classes = YourApplication.class); public class RestTemplateExampleTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.wac = MockMvcBuilders.webAppContextSetup(this.wac).build(); this.