resttemplate Can not deserialize instance of out of START_ARRAY token 或者 返回LinkHashMap类型数组

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;
}

猜你喜欢

转载自blog.csdn.net/Your1221/article/details/124951422