1、将返回数据转换为指定类型
错误写法:
@GetMapping("/list")
public List<User> listAll2() {
List<User> users = restTemplate.getForObject("http://microservice-provider-user/list-all", User.class);
for (User user : userList) {
System.out.println(user.getId());
}
return userList;
}
正确:把服务端代码改为数组接收,在将数组转化为List。
@GetMapping("/list")
public List<User> listAll2() {
User[] users = restTemplate.getForObject("http://microservice-provider-user/list-all", User[].class);
List<User> userList = Arrays.asList(users);
for (User user : userList) {
System.out.println(user.getId());
}
return userList;
}