Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

GetMapping("/rest/v1/books") in spring rest api

@GetMapping("/rest/v1/books")public ResponseEntity<List<Book>> findAllBooks() {    List<Book> books = bookService.findAll();    return ResponseEntity.ok(books);  // return 200, with json body} @GetMapping("/rest/v1/books/{bookId}")public ResponseEntity<Book> findBookById(@PathVariable long bookId) {    try {        Book book = bookService.findById(bookId);        return ResponseEntity.ok(book);  // return 200, with json body    } catch (ResourceNotFoundException ex) {        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); // return 404, with null body    }}
Source by www.dariawan.com #
 
PREVIOUS NEXT
Tagged: #spring #rest #api
ADD COMMENT
Topic
Name
3+3 =