Java 學習記錄139 — 員工管理系統專案練習-Creating Update Employee REST API

// update employee by id - version 1
@PutMapping("/employees/{id}")
public Employee updateEmployee(@PathVariable Long id, @RequestBody Employee newEmployee) {
Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Employee ID not found:" + id));
employee.setFirstName(newEmployee.getFirstName());
employee.setLastName(newEmployee.getLastName());
employee.setemailId(newEmployee.getemailId());
return employeeRepository.save(employee);
}
// update employee by id - version 2
@PutMapping("/employees/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable Long id, @RequestBody Employee newEmployee) {
Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Employee ID not found:" + id));
employee.setFirstName(newEmployee.getFirstName());
employee.setLastName(newEmployee.getLastName());
employee.setemailId(newEmployee.getemailId());
Employee updateEmployee = employeeRepository.save(employee); return ResponseEntity.ok(updateEmployee);
}
{
"firstName": "Tom1",
"lastName": "Cruise1",
"emailId": "tom_cruise@gmail.com"
}

--

--

記錄成為軟體工程師的過程

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store