Spring: Spring自带的Http客户端RestTemplate

一、介绍

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();  
    }  
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉成226

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值