我有一個微服務架構,我試圖通過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);
});
我想我正在嘗試迭代槽哈希圖,因為我正在迭代槽串列,這就是我的代碼正在剎車的原因。
我怎樣才能解決這個問題?
uj5u.com熱心網友回復:
List<Profile> foundProfile = restTemplate.exchange(
"http://localhost:8087/profile-service/profile/list",
HttpMethod.POST,
requestEntity,
new ParameterizedTypeReference<List<Profile>>() {}
).getBody();
嘗試Profile
在執行請求時將其轉換為 List of。
uj5u.com熱心網友回復:
當 Jackson 嘗試反序列化 JSON 中的物件,但沒有給出目標型別資訊時,它將使用默認型別:LinkedHashMap。
杰克遜:java.util.LinkedHashMap 不能轉換為 X
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/474895.html