(相关资料图)
原文链接
GitHub项目地址
Gitee项目地址
本文介绍restTemplate基础用法。
Java中get和post的用法请参考:
https://mp.weixin.qq.com/s/mC0D1nuCqIori5bWtLorWQ
@RestController@RequestMapping("/homepage")public class MyController { @Autowired MyService myService; // 提供get接口 @GetMapping("/provideGet") public Map provideGet(){ return myService.provideGet(); } // 提供post接口 @PostMapping("/providePost") public Map providePost(@RequestParam("number") int number, @RequestParam("name") String name) { return myService.providePost(number, name); } // 提供map参数的post接口 @PostMapping("/providePostByMap") public Map providePostByMap(@RequestParam Map map) { return myService.providePostByMap(map); } // 调用get接口 @GetMapping("/useGet") public Map useGet() { return myService.useGet(); }}
@Service@EnableSchedulingpublic class MyService { public Map provideGet() { Map res = new HashMap<>(); res.put("number", "3"); res.put("name", "张三get"); System.out.println("provideGet res:" + res + "\n"); return res; } public Map providePost(int number, String name) { Map res = new HashMap<>(); res.put("number", number); res.put("name", name); return res; } public Map providePostByMap(Map map) { int number = map.get("number") == null ? 0 : Integer.parseInt((String) map.get("number")); String name = map.get("name") == null ? "" : (String) map.get("name"); Map res = new HashMap<>(); res.put("number", number); res.put("name", name); System.out.println("providePostByMap res:" + res + "\n"); return res; }}
使用restTemplate调用get/post接口。
getForObject()
:返回值是HTTP
协议的响应体getForEntity()
:返回的是ResponseEntity
,ResponseEntity
是对HTTP
响应的封装,除了包含响应体,还包含HTTP
状态码、contentType、contentLength、Header
等信息@RestController@RequestMapping("/homepage")public class MyController { @Autowired MyService myService; // 调用get接口 @GetMapping("/useGet") public Map useGet() { return myService.useGet(); } // 调用get接口验证账号密码 @GetMapping("/useGetByPsw") public Map useGetByPsw() { return myService.useGetByPsw(); } // 调用post接口 @PostMapping("/usePost") public Map usePost() { return myService.usePost(); }}
@Service@EnableSchedulingpublic class MyService { @Resource private RestTemplate restTemplate; String getURL = "http://localhost:8081/homepage/provideGet"; String postURL = "http://localhost:8081/homepage/providePostByMap"; public Map useGet() { // getForObject返回值是HTTP协议的响应体 String strObject = restTemplate.getForObject(getURL, String.class); JSONObject jsonObject = JSONObject.parseObject(strObject); // getForEntity返回的是ResponseEntity,是对HTTP响应的封装 ResponseEntity responseData = restTemplate.getForEntity(getURL, ResponseResult.class); Map returnData = new HashMap<>(); returnData.put("StatusCode:", responseData.getStatusCode()); returnData.put("Body:", responseData.getBody()); System.out.println("useGet jsonObject:" + jsonObject + "\n"); System.out.println("useGet responseData:" + responseData + "\n"); System.out.println("useGet returnData:" + returnData + "\n"); return returnData; } public Map useGetByPsw() { RestTemplateBuilder builder = new RestTemplateBuilder(); RestTemplate restTemplate = builder.basicAuthentication("username", "password").build(); // getForEntity返回的是ResponseEntity,是对HTTP响应的封装 ResponseEntity responseData = restTemplate.getForEntity(getURL, ResponseResult.class); Map returnData = new HashMap<>(); returnData.put("StatusCode:", responseData.getStatusCode()); returnData.put("Body:", responseData.getBody()); System.out.println("useGetByPsw returnData:" + responseData + "\n"); System.out.println("useGetByPsw returnData:" + returnData + "\n"); return returnData; } public Map usePost() { //RestTemplate在postForObject时,用MultiValueMap,不可使用HashMap。 MultiValueMap sendData = new LinkedMultiValueMap<>(); sendData.add("number", "3"); sendData.add("name", "张三post"); // getForObject返回值是HTTP协议的响应体 String strObject = restTemplate.postForObject(postURL, sendData, String.class); JSONObject jsonObject = JSONObject.parseObject(strObject); // getForEntity返回的是ResponseEntity,是对HTTP响应的封装 ResponseEntity responseData = restTemplate.postForEntity(postURL, sendData, ResponseResult.class); Map returnData = new HashMap<>(); returnData.put("StatusCode:", responseData.getStatusCode()); returnData.put("Body:", responseData.getBody()); System.out.println("usePost jsonObject:" + jsonObject + "\n"); System.out.println("usePost responseData:" + responseData + "\n"); System.out.println("usePost returnData:" + returnData + "\n"); return returnData; }}
X 关闭
X 关闭
https: mp weixin qq com s mC0D1nuCqIori5bWtLorWQ
食材:秋葵4根、鸡蛋3个、盐适量、牛奶2勺、色拉油少量做法:1 秋葵洗净放沸水中煮制翠绿捞出,切掉头尾。2 取碗打鸡蛋加入牛奶盐和少量油搅拌
3月23日盘中消息,10点35分环旭电子(601231)触及涨停板。目前价格18 15,上涨10 0%。其所属行业消费电子目前上涨。领涨股为奥尼电子。该股为
1、首先打开手机里的微信,将微信版本升级至7 0 14及以上版本。2、然后打开【微信】客户端,接着点击右下角【我】图标,点击上方个人头像。3、再
点击上方蓝色文字关注我们吧根据山东开放大学和潍坊市委组织部工作安排,2023年3月18日,潍坊市村(社区)基层干部专科学历教育第二学期期末考
丘栋荣大幅减持美团最新隐形重仓股曝光,美团,丘栋荣,重仓股,正收益,基金资产净值
1、2019年的养老保险断缴了,可以自己补缴。2、第一种情况: 社保断交也分两种情况,第一种情况是你在公司入职时,公司替你缴纳一部分社保
海外网3月22日电据《每日邮报》3月21日报道,美国各大零售商将大规模关闭门店,以削减成本应对高通胀。美国运动服装零售商福洛克20日表示,将
松霖科技:关于债券持有人可转债持有比例变动达到10%的公告
1、阿华田是一种著名的饮料来自于瑞士。1904年阿华田由阿尔波特万德博士试制成功,从此誉满全球:从津巴布韦到卡塔尔,从俄罗斯到厄瓜多尔,阿