This is my service code where I am calling "http://localhost:3232/survey" and it will return List of Employees. Method getListEmployee is working fine but when I call method findEmpById it shows me the error
Service class
@Service
public class EmployeeService {
public List<Employee> getListEmployee() {
RestTemplate restTemplate = new RestTemplate();
List<Employee> data = restTemplate.getForObject("http://localhost:3232/survey", List.class);
return data;
}
public Employee findEmpById(String id) {
try {
List<Employee>data=getListEmployee();
for(Employee e1:data){
System.out.println(e1.getFirstName());
}
}catch(Exception ex) {
ex.printStackTrace();
}
return null;
}
}
I am getting error in the console when I call method **findEmpById**
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.employeeService.dto.Employee (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.employeeService.dto.Employee is in unnamed module of loader 'app')
Employee class
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Employee {
private String EmployeeID;
private String FirstName;
private String LastName;
private String Gender;
private Integer age;
private String BusinessTravel;
private String Department;
private Integer DistanceFromHome_km;
private String State;
private String Ethnicity;
private String Education;
private String EducationField;
private String JobRole;
private String MaritalStatus;
private Integer Salary;
private Integer StockOptionLevel;
private String OverTime;
private Date HireDate;
private String Attrition;
private Integer YearsAtCompany;
private Integer YearsInMostRecentRole;
private Integer YearsSinceLastPromotion;
private Integer YearsWithCurrManager;
private String id;
}
you should use a different RestTemplate
method, such as exchange
, in this way:
ResponseEntity<List<Employee>> entity = restTemplate.exchange("http://localhost:3232/survey", HttpMethod.GET, null, new ParameterizedTypeReference<List<Employee>>() {});
List<String> data = entity.getBody();
In this way, you can tell how to map the collection of items that have to be deserialized in the response.