Restaurant Retrieval Controller

In this lesson, we'll build on our restaurant retrieval service to create a REST API endpoint that allows users to fetch detailed information about a specific restaurant.

REST Endpoint Implementation

Let's implement the getRestaurant endpoint in our RestaurantController class to handle HTTP GET requests for individual restaurants.

@GetMapping("/{restaurantId}") public ResponseEntity<RestaurantDto> getRestaurant(@PathVariable String restaurantId) { return restaurantService.getRestaurant(restaurantId) .map(restaurant -> ResponseEntity.ok(restaurantMapper.toDto(restaurant))) .orElse(ResponseEntity.notFound().build()); }

Response Handling

Our endpoint needs to handle two scenarios elegantly:

When the restaurant exists, we return an HTTP 200 OK response with the restaurant details.

When the restaurant doesn't exist, we return an HTTP 404 Not Found response.

We achieve this using the Optional API's map method combined with Spring's ResponseEntity builder:

  • map() transforms the found restaurant into a DTO
  • orElse() handles the case where no restaurant is found

Summary

  • Implemented GET endpoint at /api/restaurants/{restaurantId}
  • Leveraged Optional API for clean null handling
© 2026 Devtiro Ltd. All rights reserved