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

//     delete employees by id - version 1
@DeleteMapping("/employees/{id}")
public ResponseEntity<Map<String, Boolean>> deleteEmployee(@PathVariable Long id) {
Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Employee ID not found:" + id));
employeeRepository.delete(employee);
Map<String, Boolean> response = new HashMap<>();
response.put("delete", Boolean.TRUE);
return ResponseEntity.ok(response);
}
// delete employees by id - version 2
@DeleteMapping("/employees/{id}")
public ResponseEntity<Long> deleteEmployee(@PathVariable Long id) {
Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Employee ID not found:" + id));
employeeRepository.delete(employee);
return new ResponseEntity<>(id, HttpStatus.NO_CONTENT);
}
{
"delete": true
}

--

--

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

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