Review Retrieve Controller
Now that we have implemented the service layer for retrieving individual reviews in our previous lesson, we'll create the REST API endpoint that makes this functionality available to our users.
Implementing the Controller Endpoint
The REST API endpoint for retrieving a single review needs to handle both the successful case where a review exists and the case where it doesn't.
Let's add the getRestaurantReview method to our ReviewController class:
@GetMapping("/{reviewId}")
public ResponseEntity<ReviewDto> getRestaurantReview(
@PathVariable String restaurantId,
@PathVariable String reviewId) {
return reviewService
.getRestaurantReview(restaurantId, reviewId)
.map(reviewMapper::toDto)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build());
}Response Handling
Our endpoint handles two scenarios:
-
When a review is found, we transform it through our mapper and return it with a 200 OK status.
-
When no review is found, we return a 204 No Content response, indicating the request was successful but there's no content to return.
Summary
- Implemented GET endpoint for single review retrieval
- Used
ResponseEntityto handle both found and not found cases