java.util.LinkedHashMap 无法转换为 class com.profile.model.Profile - Spring 启动

java.util.LinkedHashMap cannot be cast to class com.profile.model.Profile - Spring Boot

我有一个微服务架构,我试图通过 exchange 方法调用其他服务来获取用户配置文件列表。

@Override
    public List<Profile> getListOfProfile(List<String> receiverId) {
        ListOfProfileDto listOfProfileDto = new ListOfProfileDto();
        listOfProfileDto.setId(receiverId);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Object> requestEntity = new HttpEntity<>(listOfProfileDto, httpHeaders);
        try {
            Object foundProfile = restTemplate.exchange(
                    "http://localhost:8087/profile-service/profile/list",
                    HttpMethod.POST,
                    requestEntity,
                    Object.class
            ).getBody();
            return (List<Profile>) foundProfile;

        } 
    }

当我在代码的其他部分获得该列表时,我正在尝试对其进行迭代。

  receiversProfiles.forEach(rp -> {
            var invitedFriend = InvitedFriends.builder()
                    .firstName(rp.getFirstName())
                    .lastName(rp.getLastName())
                    .build();
            invitedFriendsList.add(invitedFriend);
        });

这里是我遇到以下错误的地方:

{
    "status": 500,
    "timeStamp": "15/05/2022 15:35:27",
    "message": "class java.util.LinkedHashMap cannot be cast to class com.profile.model.Profile (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.profile.model.Profile is in unnamed module of loader 'app')",
    "details": "uri=/profile/save"
}

当我收到其他服务的响应时,我看到了:

在哪里找到的配置文件是 hashmap,我正在尝试将其转换为 List<Profile> 但不能。

并且在我尝试迭代时的这段代码中

receiversProfiles.forEach(rp -> {
            var invitedFriend = InvitedFriends.builder()
                    .firstName(rp.getFirstName())
                    .lastName(rp.getLastName())
                    .build();
            invitedFriendsList.add(invitedFriend);
        });

我想我正在尝试通过 hashmap 进行迭代,因为我正在通过列表进行迭代,这就是我的代码停止运行的原因。

我该如何解决这个问题?

 List<Profile> foundProfile = restTemplate.exchange(
              "http://localhost:8087/profile-service/profile/list",
               HttpMethod.POST,
               requestEntity,
               new ParameterizedTypeReference<List<Profile>>() {}
).getBody();

尝试在执行请求时转换为 Profile 的列表。

当 Jackson 尝试反序列化 JSON 中的对象但未提供目标类型信息时,它将使用默认类型:LinkedHashMap。

Jackson: java.util.LinkedHashMap cannot be cast to X