Match the request and the appropriate handler:
Java
@RestController
public class LibraryController {
private List<Book> books = new ArrayList<>();
// 1
@GetMapping(path = "/books/{id}")
public Book getBookById(@PathVariable int id) {
return books.get(id - 1);
}
// 2
@GetMapping(path = "/books")
public Book getBook() {
return books.get(0);
}
// 3
@GetMapping(path = "/books/{name}")
public Book getBookByName(@PathVariable String name) {
Book book;
for (Book value : books) {
book = value;
if (book.getName().equals(name)) return book;
}
return null;
}
}
Kotlin
@RestController
class LibraryController {
private val books: List<Book> = ArrayList()
// 1
@GetMapping("/books/{id}")
fun getBookById(@PathVariable id: Int): Book {
return books[id - 1]
}
// 2
@GetMapping("/books")
fun getBook(): Book {
return books[0]
}
// 3
@GetMapping("/books/{name}")
fun getBookByName(@PathVariable name: String?): Book? {
var book: Book
for (value in books) {
book = value
if (book.getName().equals(name)) return book
}
return null
}
}