Get Post Endpoint
Building on our post management functionality from previous lessons, we'll now implement the ability for users to retrieve individual posts.
This endpoint will enable allow us to retrieve a post, in order to read the post, update or delete it.
Service Interface Definition
The PostService interface needs to define the contract for post retrieval:
public interface PostService {
// ...
Post getPost(UUID id);
}Service Implementation
Now let's implement getPost in PostServiceImpl:
@Override
public Post getPost(UUID id) {
return postRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Post does not exist with ID " + id));
}Controller Method Declaration
Finally we declare the controller endpoint:
@GetMapping(path = "/{id}")
public ResponseEntity<PostDto> getPost(
@PathVariable UUID id
) {
Post post = postService.getPost(id);
PostDto postDto = postMapper.toDto(post);
return ResponseEntity.ok(postDto);
}Summary
- Implemented post retrieval