简体   繁体   English

java.util.LinkedHashMap 不能转换为类 com.profile.model.Profile - Spring Boot

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

I have a microservice architecture where I am trying to call other service via exchange method to get list of users profiles.我有一个微服务架构,我试图通过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;

        } 
    }

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:当我收到其他服务的回复时,我看到了这个:

在此处输入图像描述

Where found profile is a hashmap that I am trying to convert to List<Profile> but not able to.找到的配置文件是我试图转换为List<Profile>但无法转换的hashmap图。

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?我怎样才能解决这个问题?

 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.尝试在执行请求时将其转换为Profile列表。

When Jackson attempts to deserialize an object in JSON, but no target type information is given, it'll use the default type: LinkedHashMap.当 Jackson 尝试反序列化 JSON 中的对象,但没有给出目标类型信息时,它将使用默认类型:LinkedHashMap。

Jackson: java.util.LinkedHashMap cannot be cast to X 杰克逊:java.util.LinkedHashMap 不能转换为 X

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Spring Boot REST API - 补丁 - “java.util.LinkedHashMap 无法转换为 xxx” - Java Spring Boot REST API - Patch - "java.util.LinkedHashMap cannot be cast to xxx" class java.util.LinkedHashMap 不能转换为 class [...] - class java.util.LinkedHashMap cannot be cast to class [...] class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String - class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String java.lang.ClassCastException:无法将java.util.LinkedHashMap强制转换为com。****。db。***** - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.****.db.***** java.lang.ClassCastException:无法将java.util.LinkedHashMap强制转换为com.example.backendtest.Project - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.example.backendtest.Project java.lang.ClassCastException:java.util.LinkedHashMap 无法转换为 com..dto.PersonDto - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com..dto.PersonDto java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法转换为java.util.LinkedHashMap - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.LinkedHashMap java.lang.ClassCastException:java.util.LinkedHashMap 无法转换为 com.testing.models.Account - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account Java Spring rest java.lang.ClassCastException:java.util.LinkedHashMap 不能转换为 Cuestionario - Java Spring rest java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to Cuestionario 在使用Java 8和Java 11时,如何修复&#39;class java.util.LinkedHashMap无法强制转换为类&#39; - How to fix 'class java.util.LinkedHashMap cannot be cast to class' when using Java 8 and Java 11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM