Skip to content

Instantly share code, notes, and snippets.

@evrentan
Created June 13, 2021 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evrentan/60ee08d4d058b6772b1773e35184d83b to your computer and use it in GitHub Desktop.
Save evrentan/60ee08d4d058b6772b1773e35184d83b to your computer and use it in GitHub Desktop.
HATEOS Usage in StudentServiceImpl.java
@Override
@Transactional(propagation = Propagation.REQUIRED)
public Student getStudentById(String studentId) {
Optional<StudentEntity> studentEntity = Optional.of(this.studentRepository.findById(studentId)).orElse(null);
if (studentEntity.isPresent()) {
Student student = this.studentMapper.toDto(studentEntity.get());
//adding hateoas links to student object
final Link selfLink = WebMvcLinkBuilder.linkTo(StudentController.class).slash(student.getId()).withSelfRel();
student.add(selfLink);
student.getLectureList().forEach(lectureRef -> {
final Link selfLectureLink = WebMvcLinkBuilder.linkTo(LectureController.class).slash(lectureRef.getId()).withSelfRel();
lectureRef.add(selfLectureLink);
});
return student;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment