一、介绍
RestTemplate 是 Spring 框架中用于发送 HTTP 请求的客户端工具类。它简化了与 REST 服务的交互,并提供了许多方法来发送 HTTP 请求,如 GET、POST、PUT、DELETE 等。
二、使用
1、添加依赖
如果你使用 Maven,确保你的项目中包含了 Spring Web 的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
注意:这个依赖通常已经包含在 Spring Boot 的 web 应用程序的起步依赖中。
2、创建 RestTemplate 实例
在配置类中创建实例。
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
或者使用 Spring Boot 2.0 之后的 RestTemplateBuilder 来创建它,这样可以更容易地进行自定义配置。
3、使用 RestTemplate
(1)GET请求
- 不带参数的get请求
@Service
public class MyService {
private final RestTemplate restTemplate;
@Autowired
public MyService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getDataFromRestService() {
String url = "http://example.com/api/data";
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
return response.getBody();
}
}
- 带请求参数的get请求
@Service
public class MyService {
private final RestTemplate restTemplate;
@Autowired
public MyService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getDataFromRestService() {
String url = "http://example.com/api/data";
HttpHeaders headers = new HttpHeaders()
// 设置后端请求Authorization认证
headers.set("Authorization", "xxx");
HttpEntity<Object> entity = new HttpEntity<>(authHeader);
// 添加请求参数,最终的url为:http://example.com/api/data?pageNum=1&pageSize=10
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
builder.queryParam("pageNum", 1);
builder.queryParam("pageSize", 10);
ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.GET, entity, Object.class);
return response.getBody();
}
}
- 带路径求情参数的get请求
@Service
public class MyService {
private final RestTemplate restTemplate;
@Autowired
public MyService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getDataFromRestService() {
String url = "http://example.com/api/data/{id}";
HttpHeaders headers = new HttpHeaders()
// 设置后端请求Authorization认证
headers.set("Authorization", "xxx");
HttpEntity<Object> entity = new HttpEntity<>(authHeader);
// 设置路径参数
Map<String, String> uriVariables = Collections.singletonMap("id", "123");
// 添加请求参数,最终的url为:http://example.com/api/data?pageNum=1&pageSize=10
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
builder.queryParam("pageNum", 1);
builder.queryParam("pageSize", 10);
ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.GET, entity, Object.class, uriVariables);
return response.getBody();
}
}
(2)POST请求
带认证的POST请求
@Service
public class MyService {
private final RestTemplate restTemplate;
@Autowired
public MyService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getDataFromRestService() {
String url = "http://example.com/api/data";
HttpHeaders headers = new HttpHeaders()
// 设置后端请求Authorization认证
headers.set("Authorization", "xxx");
HttpEntity<Object> entity = new HttpEntity<>(authHeader);
ResponseEntity<Object> response= restTemplate.postForEntity(url , entity, Object.class);
return response.getBody();
}
}