Spring RestTemplate 之exchange方法

Survive by day and develop by night.
talk for import biz , show your perfect code,full busy,skip hardness,make a better result,wait for change,challenge Survive.
happy for hardess to solve denpendies.

目录

在这里插入图片描述

概述

HTTP请求是非常常见的一种需求。

需求:

1.URL管理
2. 网页下载器
3. 爬虫调度器
4. 网页解析器
5. 数据处理器

设计思路

实现思路分析

1.POST

String reqJsonStr = “{“code”:“testCode”, “group”:“testGroup”,“content”:“testContent”, “order”:1}”;
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(reqJsonStr,headers);
ResponseEntity resp = restTemplate.exchange(DIC_DATA_URL, HttpMethod.POST, entity, Map.class);

2.(2)PUT请求

String reqJsonStr = “{“code”:“testCode”, “group”:“testGroup”,“content”:“testContent”, “order”:1}”;
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(reqJsonStr,headers);
ResponseEntity resp = restTemplate.exchange(DIC_DATA_URL, HttpMethod.POST, entity, Map.class);

3.DELETE请求

ResponseEntity resp = restTemplate.exchange(DIC_DATA_URL + “?id={id}”, HttpMethod.DELETE, null, Map.class, 227);

4.GET请求

这个也比较简单,就是对网页元素进行解析,通常利用JSONP,xpath等技术进行网页分析。

5.数据处理器

ResponseEntity results = restTemplate.exchange(url,HttpMethod.GET, null, String.class, params);

说明

说明:1)url: 请求地址;
2)method: 请求类型(如:POST,PUT,DELETE,GET);
3)requestEntity: 请求实体,封装请求头,请求内容
4)responseType: 响应类型,根据服务接口的返回类型决定
5)uriVariables: url中参数变量值

参考资料和推荐阅读

  1. https://www.cnblogs.com/jnba/p/10522608.html

欢迎阅读,各位老铁,如果对你有帮助,点个赞加个关注呗!~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值