SpringBoot使用RestTemplate

本文详细介绍了Spring的RestTemplate如何进行POST和GET请求,包括postForObject、postForEntity、exchange等方法的使用示例,以及它们之间的差异。重点讲解了如何设置Header和参数,以及GET请求的getForObject和getForEntity方法。

RestTemplate 是由 Spring 提供的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful 操作,这些操作在 RestTemplate 中都得到了实现。

POST请求

postForObject

1、使用LinkedMultiValueMap作为参数(Form表单提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";

MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("id", "123");
paramMap.add("name", "张三");
String result = template.postForObject(url, paramMap, String.class);
System.out.println("result:" + result);

2、使用Object作为参数(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";

User user = new User(123, "张三");
String result = template.postForObject(url, user, String.class);
System.out.println("result:" + result);

3、使用JSONObject作为参数(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";

JSONObject obj = new JSONObject();
obj.put("id", "123");
obj.put("name", "张三");
String result = template.postForObject(url, obj, String.class);
System.out.println("result:" + result);

postForEntity

1、使用LinkedMultiValueMap作为参数(Form表单提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";

HttpHeaders headers = new HttpHeaders();
headers.set("token", "asdf");

MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("id", "123");
paramMap.add("name", "张三");

HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers);
ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);
System.out.println("result:" + response.getBody());

2、使用Object作为参数(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";

HttpHeaders headers = new HttpHeaders();

User user = new User(123, "张三");
HttpEntity<User> httpEntity = new HttpEntity<User>(user, headers);
ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);
System.out.println("result:" + response.getBody());

3、使用JSONObject为参数(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";

HttpHeaders headers = new HttpHeaders();

JSONObject obj = new JSONObject();
obj.put("id", "123");
obj.put("name", "张三");
HttpEntity<JSONObject> httpEntity = new HttpEntity<JSONObject>(obj, headers);
ResponseEntity<String> response = template.postForEntity(url, httpEntity, String.class);
System.out.println("result:" + response.getBody());

exchange

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/productDetail";

HttpHeaders headers = new HttpHeaders();
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("id", "123");

HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers);
ResponseEntity<String> response = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
System.out.println("result:" + response.getBody());

postForObject和postForEntity方法的区别主要在于可以在postForEntity方法中设置header的属性,当需要指定header的属性值的时候,使用postForEntity方法。

exchange方法和postForEntity类似,但是更灵活,exchange还可以调用get、put、delete请求。

GET请求

getForObject

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/detail?id={id}";

Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("id", "123");

String result = template.getForObject(url, String.class, paramMap);
System.out.println("result:" + result);

getForEntity 

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/detail?id={id}";

Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("id", "123");

ResponseEntity<String> response1 = template.getForEntity(url, String.class, paramMap);
System.out.println("result:" + response1.getBody());

exchange

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/productDetail";

HttpHeaders headers = new HttpHeaders();
headers.set("token", "asdf");

HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null, headers);
ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
System.out.println("result:" + response.getBody());

Spring Boot项目中使用RestTemplate可以方便地进行HTTP请求。你可以通过创建RestTemplate实例来使用它。在你提供的代码中,你展示了两种使用RestTemplate的方式。 第一种方式是通过自己创建RestTemplate实例并设置连接超时和读取超时的方式。你可以使用SimpleClientHttpRequestFactory来设置连接超时和读取超时的时间,然后将其设置为RestTemplate的请求工厂。这样就可以创建一个自定义配置的RestTemplate实例。 第二种方式是通过使用注解@Autowired将RestTemplate实例注入到你的代码中。这种方式需要在Spring Boot项目中配置RestTemplate的Bean,然后使用@Autowired注解将其注入到需要使用的地方。 RestTemplate提供了多种方法来发送HTTP请求,包括GET请求、POST请求、PUT请求、DELETE请求等。你可以根据需要选择合适的方法来发送请求。在你提供的代码中,展示了使用postForObject方法发送POST请求的示例。你可以指定请求的URL、请求的参数和返回结果的类型,然后使用postForObject方法发送请求并获取返回结果。 总之,Spring Boot中的RestTemplate是一个方便的HTTP请求工具,可以帮助你发送各种类型的HTTP请求。你可以根据需要选择合适的方式来使用RestTemplate。 #### 引用[.reference_title] - *1* [Springboot 使用RestTemplate](https://blog.youkuaiyun.com/qq_30938705/article/details/109804221)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot使用RestTemplate](https://blog.youkuaiyun.com/watson2017/article/details/124865399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值