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

Time:05-16

I have a microservice architecture where I am trying to call other service via exchange method to get list of users profiles.

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

        } 
    }

When I get that list in other part of code I am trying to iterate it.

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

And here is where I am getting following error:

{
    "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"
}

When I am getting response from my other service I am seeing this:

enter image description here

Where found profile is a hashmap that I am trying to convert to List<Profile> but not able to.

and in this piece of code when I am trying to iterate

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

I guess I am trying to iterate trough hashmap as I am iterating trough list and this is why my code is braking.

How can I fix this?

CodePudding user response:

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

Try this to cast to List of Profile upon executing request.

CodePudding user response:

When Jackson attempts to deserialize an object in JSON, but no target type information is given, it'll use the default type: LinkedHashMap.

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

  • Related