当前位置: 首页 > 后端技术 > Java

Java中restTemplate的使用

时间:2023-04-01 13:22:42 Java

原文链接GitHub项目地址Gitee项目地址本文介绍了restTemplate的基本用法。Java中get和post的使用请参考:Java中get和post的使用1提供get/post接口1.1Controller@RestController@RequestMapping("/homepage")publicclassMyController{@AutowiredMyServicemyService;//提供获取接口@GetMapping("/provideGet")publicMapprovideGet(){returnmyService.provideGet();}//提供post接口@PostMapping("/providePost")publicMapprovidePost(@RequestParam("number")intnumber,@RequestParam("name")Stringname){returnmyService.providePost(number,姓名);}//提供map参数的post接口@PostMapping("/providePostByMap")publicMapprovidePostByMap(@RequestParamMapmap){returnmyService.providePostByMap(map);}//调用获取接口@GetMapping("/useGet")publicMapuseGet(){returnmyService.useGet();}}1.2服务@服务@EnableSchedulingpublicclassMyService{publicMapprovideGet(){Mapres=newHashMap<>();res.put("数字","3");res.put("name","张三get");System.out.println("提供获取资源:"+res+"\n");返回资源;}publicMapprovidePost(intnumber,Stringname){Mapres=newHashMap<>();res.put("数字",数字);res.put("名字",名字);返回资源;}publicMapprovidePostByMap(Mapmap){intnumber=map.get("number")==null?0:Integer.parseInt((String)map.get("number"));字符串名称=map.get("名称")==null?“”:(字符串)map.get(“名称”);Mapres=new哈希图<>();res.put("数字",数字);res.put("名字",名字);System.out.println("providePostByMapres:"+res+"\n");返回资源;}}2调用get/post接口使用restTemplate调用get/post接口getForObject():返回值是HTTP协议的响应体。getForEntity():返回的是ResponseEntity。ResponseEntity是对HTTP响应的封装。除了响应体,还包括HTTP状态码、contentType、contentLength、Header等信息。2.1Controller@RestController@RequestMapping("/homepage")publicclassMyController{@AutowiredMyServicemyService;//调用获取接口@GetMapping("/useGet")publicMapuseGet(){returnmyService.useGet();}//调用get接口验证账号密码@GetMapping("/useGetByPsw")publicMapuseGetByPsw(){returnmyService.useGetByPsw();}//调用post接口@PostMapping("/usePost")publicMapusePost(){returnmyService.usePost();}}2.2Service@Service@EnableSchedulingpublicclassMyService{@ResourceprivateRestTemplaterestTemplate;StringgetURL="http://localhost:8081/homepage/provideGet";StringpostURL="http://localhost:8081/homepage/providePostByMap";publicMapuseGet(){//getForObject的返回值为HTTP协议响应体StringstrObject1=restTemplate.getForObject(getURL,String.class);//无参数JSONObjectjsonObject1=JSONObject.parseObject(strObject1);MultiValueMapsendData=newLinkedMultiValueMap<>();sendData.add("number","3");sendData.add("name","张三帖子");StringstrObject2=restTemplate.getForObject(getURL,String.class,sendData);//带参数的JSONObjectjsonObject2=JSONObject.parseObject(strObject2);//getForEntity返回ResponseEntity,是对HTTP响应的封装ResponseEntityresponseData=restTemplate.getForEntity(getURL,ResponseResult.class);MapreturnData=newHashMap<>();returnData.put("StatusCode:",responseData.getStatusCode());returnData.put("t;Body:",responseData.getBody());System.out.println("useGetjsonObject1:"+jsonObject1+"\n");System.out.println("useGetjsonObject2:"+jsonObject2+"\n");System.out.println("useGetresponseData:"+responseData+"\n");System.out.println("useGetreturnData:"+returnData+"\n");returnreturnData;}publicMapuseGetByPsw(){RestTemplateBuilderbuilder=newRestTemplateBuilder();RestTemplaterestTemplate=builder.basicAuthentication("username","password").build();//getForEntity返回的是ResponseEntity,是对HTTP响应的封装ResponseEntityresponseData=restTemplate.getForEntity(getURL,ResponseResult.class);MapreturnData=newHashMap<>();returnData.put("StatusCode:",响应Data.getStatusCode());returnData.put("正文:",responseData.getBody());System.out.println("useGetByPsw返回数据:"+responseData+"\n");System.out.println("useGetByPsw返回数据:"+returnData+"\n");返回返回数据;}publicMapusePost(){//RestTemplate在postForObject时,用MultiValueMap,不可使用HashMapMultiValueMapsendData=newLinkedMultiValueMap<>();sendData.add("数字","3");sendData.add("姓名","张三职位");//getForObject的返回值为HTTP协议响应体StringstrObject=restTemplate.postForObject(postURL,sendData,String.class);JSONObjectjsonObject=JSONObject.parseObject(strObject);//getForEntity返回ResponseEntity,它是对HTTP响应的封装ResponseEntityresponseData=restTemplate.postForEntity(postURL,sendData,ResponseResult.class);MapreturnData=newHashMap<>();returnData.put("状态码:",responseData.getStatusCode());returnData.put("正文:",responseData.getBody());System.out.println("使用PostjsonObject:"+jsonObject+"\n");System.out.println("usePostresponseData:"+responseData+"\n");System.out.println("usePostreturnData:"+returnData+"\n");returnreturnData;}}了解更多编程知识,请关注我的公众号:代码