I cannot map entity to dto by using map()
method of Java Stream API.
public EmployeeDto create(EmployeeRequest request) {
final Employee employee = EmployeeRequestMapper.mapToEntity(request);
return employeeRepository.save(employee)
.map(EmployeeDto::new);
}
But I get "Cannot resolve method 'map' in 'T'" error for the map method in my service. Here are the related classes below:
public class EmployeeRequestMapper {
public static Employee mapToEntity(EmployeeRequest request) {
return new Employee(
request.getName(),
request.getEmail(),
request.getCountry(),
request.getAge()
);
}
}
@Data
@NoArgsConstructor
public class EmployeeDto {
private Long id;
private String name;
private String email;
private String country;
private int age;
public EmployeeDto(Employee employee) {
this.id = employee.getId();
this.name = employee.getName();
this.email = employee.getEmail();
this.country = employee.getCountry();
this.age = employee.getAge();
}
}
So, what is the problem?
CodePudding user response:
save
returns an Employee
, not an Optional<Employee>
. And Employee
doesn't appear to have a map
method.
So your method return new EmployeeDto(employeeRepository.save(employee));
instead.