我正在Spring Boot Service类中创建UserDetails列表,然后返回Controller类。
在控制器类中,当我尝试执行以下操作时,
List<UserDetails> userList = userManagemetService.getEditableUsersList(user.getUserId());
System.out.println("userList " + userList + " size " + userList.size());
System.out.println("userList class 1" + (UserDetails)userList.get(0));
System.out.println("userList class 2" + userList.get(0).getClass().getName());
我收到错误
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.mx.model.usermgmt.UserDetails
更新
添加了方法定义-从定义中可以看出,我没有在任何地方使用LinkedHashmap
public List<UserDetails> getEditableUsersList(String userId) {
//Get the details of the user
List<UserDetails> editableUsers = new ArrayList<>();
try {
RestResponse<UserDetails> mxUser = this.restFactory.getRestClient(UM_REST_CLIENT_ID)
.get(RestLocation.withPath(UM_REST_VERSION + UM_REST_USER_RESOURCE).path(userId),
RestHeaders.empty().accept(MediaType.APPLICATION_JSON_VALUE),
UserDetails.class);
if (mxUser.getStatus() == HttpStatus.OK.value()) {
UserDetails userDetails = mxUser.getEntity();
System.out.println("mxUserEntity " + userDetails);
MastersUser mastersUser = userDetails.getmxUser();
UserREF xrefUser = userDetails.getUserREF();
System.out.println("mastersUser.getUserId() " + mastersUser.getUserId() + " mastersUser.getUserEmail() " + mastersUser.getUserEmail());
System.out.println("xrefUser.getRole() " + xrefUser.getRole());
try {
ObjectMapper Obj = new ObjectMapper();
// get Oraganisation object as a json string
String jsonStr = Obj.writeValueAsString(userDetails);
// Displaying JSON String
System.out.println(jsonStr);
} catch (IOException e) {
e.printStackTrace();
}
// Check if supervisor
boolean isSupervisor = isAdmin(xrefUser.getRole());
// If true, get the list of all users for all shipto accounts
if(isSupervisor){
//Get a list of childShipTo accounts for the user
List<String> shipToAccounts = getShipToAccountsList(xrefUser.getChildShipToAccounts());
System.out.println("list of accounts");
shipToAccounts.forEach(System.out::println);
List<UserDetails> usersList = shipToAccounts
.stream().map(shipTo -> getUsersForShipTo(shipTo))
.flatMap(Collection::stream)
.collect(Collectors.toList());
editableUsers = usersList;
}
// Otherwise add the user to the list and return
else{
editableUsers.add(userDetails);
}
}
} catch (IOException ioe) {
logger.info("Error fulfilling send credentials request -> ", ioe);
}
return editableUsers;
}
private List<UserDetails> getUsersForShipTo(String shipTo) {
List<UserDetails> shipToUsersList = null;
try {
RestResponse<List> shiptoUsers = this.restFactory.getRestClient(UM_REST_CLIENT_ID)
.get(RestLocation.withPath(UM_REST_VERSION +UM_REST_SHIPTO_USERS_RESOURCE).path(shipTo),
RestHeaders.empty().accept(MediaType.APPLICATION_JSON_VALUE),
List.class);
if (shiptoUsers.getStatus() == HttpStatus.OK.value()) {
shipToUsersList = (List<UserDetails>)shiptoUsers.getEntity();
}
} catch (IOException ioe) {
logger.info("Error fulfilling send credentials request -> ", ioe);
}
try {
ObjectMapper Obj = new ObjectMapper();
// get Oraganisation object as a json string
String jsonStr = Obj.writeValueAsString(shipToUsersList);
// Displaying JSON StringusersList
System.out.println("shipToUsersList " + jsonStr);
} catch (IOException e) {
e.printStackTrace();
}
return shipToUsersList;
}
我在做什么错?我该如何解决这个问题?
谢谢