@RequestMapping(value="/getUser",method = RequestMethod.GET)
public User getUser() {
ResponseEntity<User> responseEntity= restTemplate.getForEntity("http://HELLO-SERVICE/getUser", User.class);
User body = responseEntity.getBody();
return body;
}
有参数传递
@RequestMapping(value="/getUserObject",method = RequestMethod.GET)
public User getUserObject() {
User responseEntity= restTemplate.getForObject("http://HELLO-SERVICE/getUser?name={1}", User.class,"zzj");
return responseEntity;
}
通过Map对象封装参数
@RequestMapping(value="/getUserByMap",method = RequestMethod.GET)
public User getUserByMap() {
Map<String,String> map =new HashMap<>();
map.put("name", "zzj");
User responseEntity= restTemplate.getForObject("http://HELLO-SERVICE/getUser?name={name}", User.class,map);
return responseEntity;
}
POST请求
POST和GET一样,有postForEntity和postForObject方法,参数大致相同
@RequestMapping(value="/user",method = RequestMethod.POST)
public boolean save() {
User user = new User();
user.setName("abc");
user.setAge(111);
return restTemplate.postForObject("http://HELLO-SERVICE/user", user, boolean.class);
}