Last active
April 12, 2026 13:19
-
-
Save Lackoftactics/90c9e570fdc3ab4540254808f4ae88dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @PutMapping("/{studentId}") | |
| public ResponseEntity<Void> updateStudent( | |
| @PathVariable Long studentId, | |
| @Valid @RequestBody StudentRequest request) { | |
| Student existing = studentRepository.findById(studentId) | |
| .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); | |
| updateFrom(existing, request); // or maybe more compact but less easy to read: modelMapper.map(request, existing); | |
| studentRepository.save(existing); | |
| return ResponseEntity.noContent().build(); | |
| } | |
| private void updateFrom(Student student, StudentRequest request) { | |
| student.setFirstName(request.getFirstName()); | |
| student.setLastName(request.getLastName()); | |
| student.setEnrolled(request.getEnrolled()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Data | |
| @NoArgsConstructor | |
| @AllArgsConstructor | |
| public class StudentRequest { | |
| @NotBlank | |
| private String firstName; | |
| @NotBlank | |
| private String lastName; | |
| @NotNull | |
| private Boolean enrolled; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment