Created
June 13, 2021 19:23
-
-
Save evrentan/60ee08d4d058b6772b1773e35184d83b to your computer and use it in GitHub Desktop.
HATEOS Usage in StudentServiceImpl.java
This file contains 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
@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