Skip to content

Instantly share code, notes, and snippets.

@Lackoftactics
Last active April 12, 2026 13:19
Show Gist options
  • Select an option

  • Save Lackoftactics/90c9e570fdc3ab4540254808f4ae88dc to your computer and use it in GitHub Desktop.

Select an option

Save Lackoftactics/90c9e570fdc3ab4540254808f4ae88dc to your computer and use it in GitHub Desktop.
@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());
}
@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