(资料图)
原文链接
GitHub项目地址
Gitee项目地址
本文介绍restTemplate基础用法。
Java中get和post的用法请参考:Java中Get和Post的使用
@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 strObject1 = restTemplate.getForObject(getURL, String.class); //无参 JSONObject jsonObject1 = JSONObject.parseObject(strObject1); MultiValueMap sendData = new LinkedMultiValueMap<>(); sendData.add("number", "3"); sendData.add("name", "张三post"); String strObject2 = restTemplate.getForObject(getURL, String.class, sendData); // 带参 JSONObject jsonObject2 = JSONObject.parseObject(strObject2); // 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 jsonObject1:" + jsonObject1 + "\n"); System.out.println("useGet jsonObject2:" + jsonObject2 + "\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 关闭
科学家培育出不传播疟疾的蚊子 可阻止致命寄生虫传播并消除疟疾传播
2023-01-29“毅力”号在火星发现古老生命痕迹 是首次从火星返回样本
2023-01-29欧盟提出紧急措施干预高价能源 对能源超额利润征税
2023-01-29升温超1.5℃可引发7个气候临界点 改变就会不可逆转
2023-01-29韩国核聚变反应堆30秒达到1亿摄氏度 提高了核心等离子体温度
2023-01-29全球大多数碳捕获项目未达标 一些二氧化碳捕获量仅为承诺的一半
2023-01-29渔业活动是海洋垃圾带“罪魁祸首” 86%是被渔船遗弃或丢失物品
2023-01-29头发为何乱蓬蓬?可能患有遗传引起的蓬发综合征
2023-01-29超热蒸汽洗碗25秒杀死细菌 可作为医疗器械消毒
2023-01-29“悬浮”纳米粒子助力量子计算 或为精密灵敏的测量设备铺平道路
2023-01-29