- 主页 > 生活百科 > >
手撸了一个网络请求工具类,开发速度迅速提升了300%( 二 )
- 最后,我们来做一下单元测试,使用案例如下,接口api还是基于上篇文章提供的服务
@RunWith(SpringRunner.class)@SpringBootTestpublic class HttpControllerJunit {@Autowiredprivate HttpTemplate httpTemplate;/*** get请求测试*/@Testpublic void testGet(){//请求地址String url = "http://localhost:8080/testGet";//发起请求,直接返回对象ResponseBean responseBean = httpTemplate.get(url, createHeader("get"), ResponseBean.class);System.out.println(responseBean.toString());}/*** get请求测试,restful风格*/@Testpublic void testGetByRestFul(){//请求地址String url = "http://localhost:8080/testGetByRestFul/{1}";//发起请求,直接返回对象(restful风格)ResponseBean responseBean = httpTemplate.get(url, createHeader("testGetByRestFul"), ResponseBean.class, "张三");System.out.println(responseBean.toString());}/*** 模拟表单提交,post请求*/@Testpublic void testPostByForm(){//请求地址String url = "http://localhost:8080/testPostByFormAndObj";//表单参数Map<String, Object> paramMap = new HashMap<>();paramMap.put("userName", "唐三藏");paramMap.put("userPwd", "123456");//发起请求ResponseBean responseBean = httpTemplate.postByFrom(url, createHeader("testPostByFormAndObj"), paramMap, ResponseBean.class);System.out.println(responseBean.toString());}/*** 模拟JSON提交,post请求*/@Testpublic void testPostByJson(){//请求地址String url = "http://localhost:8080/testPostByJson";//入参RequestBean request = new RequestBean();request.setUserName("唐三藏");request.setUserPwd("123456789");//发送post请求,并打印结果,以String类型接收响应结果JSON字符串ResponseBean responseBean = httpTemplate.postByJson(url, createHeader("testPostByJson"), request, ResponseBean.class);System.out.println(responseBean.toString());}/*** 重定向,post请求,json方式提交*/@Testpublic void testPostByLocation(){//请求地址String url = "http://localhost:8080/testPostByLocation";//入参RequestBean request = new RequestBean();request.setUserName("唐三藏");request.setUserPwd("123456789");//用于提交完成数据之后的页面跳转String uri = httpTemplate.postForLocation(url,createHeader("testPostByLocation"), request);System.out.println(uri);}/*** put请求,json方式提交*/@Testpublic void testPutByJson(){//请求地址String url = "http://localhost:8080/testPutByJson";//入参RequestBean request = new RequestBean();request.setUserName("唐三藏");request.setUserPwd("123456789000");//模拟JSON提交,put请求ResponseBean responseBean = httpTemplate.put(url,createHeader("testPutByJson"), request, ResponseBean.class);System.out.println(responseBean.toString());}/*** delete请求,json方式提交*/@Testpublic void testDeleteByJson(){//请求地址String url = "http://localhost:8080/testDeleteByJson";//模拟JSON提交,delete请求ResponseBean responseBean = httpTemplate.delete(url,createHeader("testDeleteByJson"), ResponseBean.class);System.out.println(responseBean.toString());}/*** 文件上传,post请求*/@Testpublic void uploadFile(){//需要上传的文件String filePath = "/Users/panzhi/Desktop/Jietu20220205-194655.jpg";//请求地址String url = "http://localhost:8080/upload";//提交参数设置MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();param.add("uploadFile", new FileSystemResource(new File(filePath)));//服务端如果接受额外参数,可以传递param.add("userName", "张三");ResponseBean responseBean = httpTemplate.uploadFile(url, createHeader("uploadFile"), param, ResponseBean.class);System.out.println(responseBean.toString());}/*** 小文件下载* @throws IOException*/@Testpublic void downloadFile() throws IOException {String userName = "张三";String fileName = "f9057640-90b2-4f86-9a4b-72ad0e253d0d.jpg";//请求地址String url = "http://localhost:8080/downloadFile/{1}/{2}";//发起请求,直接返回对象(restful风格)byte[] stream = httpTemplate.downloadFile(url, createHeader("downloadFile"), userName,fileName);// 将下载下来的文件内容保存到本地String targetPath = "/Users/panzhi/Desktop/"+ fileName;Files.write(Paths.get(targetPath), Objects.requireNonNull(stream, "未获取到下载文件"));}/*** 大文件下载* @throws IOException*/@Testpublic void downloadBigFile() {String userName = "张三";String fileName = "f9057640-90b2-4f86-9a4b-72ad0e253d0d.jpg";String targetPath = "/Users/panzhi/Desktop/"+ fileName;//请求地址String url = "http://localhost:8080/downloadFile/{1}/{2}";//对响应进行流式处理而不是将其全部加载到内存中httpTemplate.downloadBigFile(url, createHeader("downloadBigFile"), clientHttpResponse -> {Files.copy(clientHttpResponse.getBody(), Paths.get(targetPath));return null;}, userName, fileName);}/*** 自定义请求头部* @param value* @return*/private Map<String, String> createHeader(String value){Map<String, String> headers = new HashMap<>();headers.put("token", value);return headers;}}
推荐阅读
-
中国小康网|英媒:英国首相跟随美国改口攻击华为 称为潜在敌国供应商
-
胸腔厚度只有身体宽度的1/3,有能改变胸腔厚度的方法吗?
-
「三九贴一冬 疼痛去无踪」山东省立三院“三九贴”开始预约啦
-
百度App新增高考官方服务 可快速查分、查录取结果等
-
Unic牌婴儿竹纤维内衣:560套Unic牌婴儿竹纤维内衣套存缺陷 公司召回
-
-
|《一一》是一部平淡的影片,但它所呈现的生活状态,只有少数人能读懂。
-
【狗狗】狗为什么会“哭”?它会用流泪来表达悲伤吗?你家狗会哭吗
-
-
津会讲娱乐|最近爆火的古风改良版仙女裙,仙气十足,草莓味儿的小仙女最爱
-
东京奥组委发言人暗示:不排除奥运会闭门举行的可能性
-
陕西都市快报|西安一小区楼上楼下邻居互相报警,竟是因为…
-
陈乔恩探望安以轩女儿,|画面非常有爱!陈乔恩探望安以轩女儿 忍不住拿出手机拍拍拍
-
[虎牙直播 虎牙童子 王者荣耀 赵云 国服赵云童子 斗鱼直播 游戏]|虎牙童子合同纠纷败诉,需赔偿1300万,含泪向网友鞠躬道别,无限期停播
-
江苏新闻微信公号|正在商谈征收补偿的房屋一夜被拆财物不翼而飞,街道承认偷拆
-
【经济观察网|目标不只是短期加码618,京东零售牵手快手共建供应链】
-
-
3DMGAME|特别好评!开放世界生存模拟新游《中世纪王朝》登陆Steam
-
京广北路隧道排水现场的安徽力量:“90后”挑大梁,人歇机不停!
-