RestTemplate调用接口总结
程序员文章站
2022-06-26 08:01:13
...
1.
这是接口信息
@PostMapping("/testm")
public ReturnResult show11(@RequestParam("id") String id,
@RequestParam("name") String name) {
System.out.println(id);
UserInfo userInfo = userInfoMapper.selectByUserName(name);
return ReturnResult.create(userInfo);
}
这是restTemplate调用
//Post,@RequestParam---postForEntity
@Test
public void sho11() {
String url = "http://127.0.0.1:8099/user/testm";
MultiValueMap<String, String> request = new LinkedMultiValueMap<>();
request.add("id", "12324");
request.add("name", "nanc");
ResponseEntity<ReturnResult> resp = restTemplate.postForEntity(url, request, ReturnResult.class);
ReturnResult body = resp.getBody();
UserInfo data = body.getData();
System.err.println(data.getUserId());
System.out.println(data);
}
2.
接口
@GetMapping("/testp")
public ReturnResult show22(@RequestParam String name,
@RequestParam Integer age,
@RequestParam String clazz) {
System.out.println(name + "-" + age + "-" + clazz);
UserInfo userInfo = userInfoMapper.selectByUserName(name);
return ReturnResult.create(userInfo);
}
resttemplate
//Get ,@RequestParam-----getForObject
@Test
public void sho12() {
String url = "http://127.0.0.1:8099/rest/testp?name={name}&age={age}&clazz={clazz}";
Map<String, Object> map = new HashMap<>();
map.put("name", "nanc");
map.put("age", 34);
map.put("clazz", "12");
ReturnResult forObject = restTemplate.getForObject(url, ReturnResult.class, map);
UserInfo data = forObject.getData();
System.out.println(data);
}