服务消费者:
@RequestMapping("/web/getuser")
public User getuser(){
String[] strArry={"105","张无忌","136"};
return restTemplate.getForEntity("http://SPRINGCLOUD-001-PROVIDER/service/getuser?id={0}&name={1}&phone={2}", User.class,paramMap).getBody();
}
@RequestMapping("/web/getuser")
public User getuser(){
Map<String, Object> paramMap = new ConcurrentHashMap<>();
paramMap.put("id", 1028);
paramMap.put("name", "张翠山");
paramMap.put("phone","1340000000");
return restTemplate.getForEntity("http://SPRINGCLOUD-001-PROVIDER/service/getuser?id={id}&name={name}&phone={phone}", User.class,paramMap).getBody();
}
@RequestMapping("/service/getuser")//只支持get请求
public User getuser(@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam("phone") String phone){
System.out.println("服务提供者2-----------2222");
User user=new User();
user.setId(id);
user.setName(name);
user.setPhone(phone);
return user;
}
与 getForEntity 使用类似,只不过 getForObject 是在
getForEntity 基础上进行了再次封装,可以将 http 的响应体 body
信息转化成指定的对象,方便我们的代码开发;
当你不需要返回响应中的其他信息,只需要 body 体信息的时候,可以
使用这个更方便;
get中,数组和map都可以
但是在post中,只能用:MultiValueMap
//要传的表单信息,参数数据(很坑人)
MultiValueMap dataMap=new LinkedMultiValueMap();
dataMap.add("id","12346");
dataMap.add("name","POST方法传递数据");
dataMap.add("phone","5640000000");
//调用spring cloud服务提供者提供的服务
//return restTemplate.getForEntity("http://localhost:8080/service/hello",String.class).getBody();
ResponseEntity<User> responseEntity=restTemplate.postForEntity("http://SPRINGCLOUD-001-PROVIDER/service/adduser",dataMap, User.class);
User body1=restTemplate.postForObject("http://SPRINGCLOUD-001-PROVIDER/service/adduser",dataMap, User.class);
具体代码如下:
1.服务消费者:
@RequestMapping("/web/adduser")
public User adduser(){
String[] strArry={"105","张无忌","136"};
Map<String, Object> paramMap = new ConcurrentHashMap<>();
paramMap.put("id", 1028);
paramMap.put("name", "张翠山");
paramMap.put("phone","1340000000");
//要传的表单信息,参数数据(很坑人)
MultiValueMap dataMap=new LinkedMultiValueMap();
dataMap.add("id","12346");
dataMap.add("name","POST方法传递数据");
dataMap.add("phone","5640000000");
//调用spring cloud服务提供者提供的服务
//return restTemplate.getForEntity("http://localhost:8080/service/hello",String.class).getBody();
ResponseEntity<User> responseEntity=restTemplate.postForEntity("http://SPRINGCLOUD-001-PROVIDER/service/adduser",dataMap, User.class);
User body1=restTemplate.postForObject("http://SPRINGCLOUD-001-PROVIDER/service/adduser",dataMap, User.class);
int statusCodeValue=responseEntity.getStatusCodeValue();
HttpStatus httpStatus=responseEntity.getStatusCode();
HttpHeaders httpHeaders=responseEntity.getHeaders();
User body2=responseEntity.getBody();
System.out.println(body2.getId()+"--"+body2.getName()+"--"+body2.getPhone());
System.out.println(statusCodeValue);
System.out.println(httpStatus);
System.out.println(httpHeaders);
System.out.println(body1.getId()+"--"+body1.getName()+"--"+body1.getPhone());
return restTemplate.postForEntity("http://SPRINGCLOUD-001-PROVIDER/service/adduser",dataMap, User.class).getBody();
}
服务提供者:
//@RequestMapping(value = "/service/adduser",method = RequestMethod.POST)//只支持get请求
@PostMapping("/service/adduser")
public User adduser(@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam("phone") String phone){
System.out.println("服务提供者1-----------1add");
User user=new User();
user.setId(id);
user.setName(name);
user.setPhone(phone);
//将user对象插入数据库(暂时省略)
return user;
}