In the snippets below, we create the Restaurant class with some useful information like the name, address, type; RestaurantController with the getRestaurant method which returns a Restaurant object by its id which will be the index of that object in restaurantList:
Java
public class Restaurant {
private String name;
private String address;
private String type;
// constructor
// getters and setters
}
@RestController
public class RestaurantController {
private final List<Restaurant> restaurantList;
public RestaurantController() {
restaurantList = List.of(
new Restaurant("Gandhi Cafe", "Germany, Stuttgart, Sonner str 8", "cafe"),
new Restaurant("Aloha", "Spain, Valencia, Carrer de Vilafermosa 3", "bar")
);
}
@GetMapping("restaurants/{id}")
public Restaurant getRestaurant(@PathVariable int id) {
return restaurantList.get(id); // change this
}
}
Kotlin
class Restaurant(
val name: String,
val address: String,
val type: String
)
@RestController
class RestaurantController {
private val restaurantList = listOf(
Restaurant("Gandhi Cafe", "Germany, Stuttgart, Sonner str 8", "cafe"),
Restaurant("Aloha", "Spain, Valencia, Carrer de Vilafermosa 3", "bar")
)
@GetMapping("restaurants/{id}")
fun getRestaurant(@PathVariable id: Int): Restaurant {
return restaurantList[id] // change this
}
}
Imagine a situation when we have a negative id in @PathVariable . It produces exceptions that we don't handle. How can we change our code to avoid this? Select all correct answers.
Java
-
@GetMapping("restaurants/{id}") public Restaurant getRestaurant(@PathVariable int id) { if (id < 0) { throw new ResponseStatusException("Id is incorrect"); } return restaurantList.get(id); } -
@GetMapping("restaurants/{id}") public Restaurant getRestaurant(@PathVariable int id) { if (id < 0) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Id is incorrect"); } return restaurantList.get(id); } -
@ResponseStatus(code = HttpStatus.BAD_REQUEST) class RestaurantNotFoundException extends RuntimeException { // ... } // ... @GetMapping("restaurants/{id}") public Restaurant getRestaurant(@PathVariable int id) { if (id < 0) { throw new RestaurantNotFoundException("Id is incorrect"); } return restaurantList.get(id); } -
@GetMapping("restaurants/{id}") public Restaurant getRestaurant(@PathVariable int id) { if (id < 0) { throw new RestaurantNotFoundException("Id is incorrect"); } return restaurantList.get(id); }
Kotlin
-
@GetMapping("restaurants/{id}") fun getRestaurant(@PathVariable id: Int): Restaurant { if (id < 0) { throw ResponseStatusException("Id is incorrect") } return restaurantList[id] } -
@GetMapping("restaurants/{id}") fun getRestaurant(@PathVariable id: Int): Restaurant { if (id < 0) { throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Id is incorrect") } return restaurantList[id] } -
@ResponseStatus(code = HttpStatus.BAD_REQUEST) class RestaurantNotFoundException : RuntimeException() { // ... } // . . . @GetMapping("restaurants/{id}") fun getRestaurant(@PathVariable id: Int): Restaurant? { if (id < 0) { throw RestaurantNotFoundException("Id is incorrect") } return restaurantList[id] } -
@GetMapping("restaurants/{id}") fun getRestaurant(@PathVariable id: Int): Restaurant? { if (id < 0) { throw RestaurantNotFoundException("Id is incorrect") } return restaurantList[id]