I've already checked similar questions, but none of them wasn't helpful for me:
I have the following code in service implementation with business-logic for the project as:
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
EmployeeRepository employeeRepository;
@Override
public MessageResponse createEmployee(EmployeeRequest employeeRequest) {
Employee newEmployee = new Employee();
newEmployee.setFirstName(employeeRequest.getFirstName());
newEmployee.setLastname(employeeRequest.getLastname());
newEmployee.setPhoneNumber(employeeRequest.getPhoneNumber());
newEmployee.setEmail(employeeRequest.getEmail());
newEmployee.setSalary(employeeRequest.getSalary());
newEmployee.setDepartment(employeeRequest.getDepartment());
employeeRepository.save(newEmployee);
return new MessageResponse("New Employee created successfully");
}
@Override
public Optional<Employee> updateEmployee(Integer employeeId, EmployeeRequest employeeRequest) throws ResourceNotFoundException {
Optional<Employee> employee = employeeRepository.findById(employeeId);
if (employee.isEmpty()) {
throw new ResourceNotFoundException("Employee", "id", employeeId);
} else {
employee.get().setFirstName(employeeRequest.getFirstName());
employee.get().setLastname(employeeRequest.getLastname());
employee.get().setPhoneNumber(employeeRequest.getPhoneNumber());
employee.get().setEmail(employeeRequest.getEmail());
employee.get().setSalary(employeeRequest.getSalary());
employee.get().setDepartment(employeeRequest.getDepartment());
employeeRepository.save(employee.get());
return employee;
}
}
@Override
public Employee getASingleEmployee(Integer employeeId) throws ResourceNotFoundException {
return employeeRepository.findById(employeeId)
.orElseThrow(() -> new ResourceNotFoundException("Employee", "id", employeeId));
}
@Override
public List<Employee> getAllEmployee() {
return employeeRepository.findAll();
}
@Override
public void deleteEmployee(Integer employeeId) throws ResourceNotFoundException {
if (employeeRepository.getById(employeeId).getId().equals(employeeId)) {
employeeRepository.deleteById(employeeId);
} else throw new ResourceNotFoundException("Employee", "id", employeeId);
}
}
The APIs controller with the code as:
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/all")
public ResponseEntity<List<Employee>> getAllEmployees() {
List<Employee> employees = employeeService.getAllEmployee();
return new ResponseEntity<>(employees, HttpStatus.OK);
}
@GetMapping("/find/{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable("id") Integer id) {
Employee employee = employeeService.getASingleEmployee(id);
return new ResponseEntity<>(employee, HttpStatus.OK);
}
@PostMapping("/add")
public ResponseEntity<MessageResponse> addEmployee(@RequestBody EmployeeRequest employee) {
MessageResponse newEmployee = employeeService.createEmployee(employee);
return new ResponseEntity<>(newEmployee, HttpStatus.CREATED);
}
@PutMapping("/update/{id}")
public Optional<Employee> updateEmployee(@PathVariable Integer id, @RequestBody EmployeeRequest employee) {
return employeeService.updateEmployee(id, employee);
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<?> deleteEmployee(@PathVariable("id") Integer id) {
employeeService.deleteEmployee(id);
return new ResponseEntity<>(HttpStatus.OK);
}
}
In POM
I had:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
While working with the project, I've reproduced the issue as:
Cannot resolve method 'getById' in 'EmployeeRepository'
After some investigations, I've found the cause and the way how can I fix this issue.
The cause of issue was the version of the dependency as:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
To fix it, I needed to change the version to higher, for example, to:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
or to another one as:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
but for the usage of 2.7.5
version, you need to be careful and aware , because:
getById(ID)
is deprecated and you should use instead getReferenceById(ID) .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.