1616@ RestController
1717@ RequestMapping ("/book" )
1818public class BookController {
19- private static List <Book > bookList = new ArrayList <>();
19+ private static final List <Book > bookList = new ArrayList <>();
2020
2121 static {
22- bookList .add (Book .builder ().id (12345 ).author ("Rao" ).name ("Black book" ).build ());
23- bookList .add (Book .builder ().id (12346 ).author ("Gosling" ).name ("Java" ).build ());
22+ bookList .add (Book .builder ().id (12345L ).author ("Rao" ).name ("Black book" ).build ());
23+ bookList .add (Book .builder ().id (12346L ).author ("Gosling" ).name ("Java" ).build ());
2424
2525 }
2626
2727 //localhost:8384/book/list
2828 @ GetMapping ("/list" )
2929 public ResponseEntity <List <Book >> getBookList () {
30- ResponseEntity responseEntity = new ResponseEntity <>(bookList , HttpStatus .OK );
30+ ResponseEntity < List < Book >> responseEntity = new ResponseEntity <>(bookList , HttpStatus .OK );
3131 log .info (responseEntity );
3232 return responseEntity ;
3333 }
@@ -38,11 +38,11 @@ public ResponseEntity<List<Book>> getBookList() {
3838 public ResponseEntity <Book > getBookById (@ PathVariable int id ) {
3939 Optional <Book > optionalBook = bookList .stream ().filter (b -> b .getId () == id ).findAny ();
4040 if (optionalBook .isPresent ()) {
41- ResponseEntity responseEntity = new ResponseEntity <Book >(optionalBook .get (), HttpStatus .OK );
41+ ResponseEntity < Book > responseEntity = new ResponseEntity <>(optionalBook .get (), HttpStatus .OK );
4242 log .info (responseEntity );
4343 return responseEntity ;
4444 }
45- ResponseEntity responseEntity1 = new ResponseEntity <>(Book .builder ().build (), HttpStatus .NOT_FOUND );
45+ ResponseEntity < Book > responseEntity1 = new ResponseEntity <>(Book .builder ().build (), HttpStatus .NOT_FOUND );
4646 log .info (responseEntity1 );
4747 return responseEntity1 ;
4848 }
@@ -66,12 +66,11 @@ public ResponseEntity<Book> createBook(@Valid @RequestBody Book book) {
6666 // localhost:8384/book/update
6767 @ PutMapping ("/update" )
6868 public ResponseEntity <Book > updateBook (@ RequestBody Book book ) {
69- Book updatedUser = bookList .stream ().filter (u ->u .getId ()== book .getId ()). map (up ->{
69+ Book updatedUser = bookList .stream ().filter (u -> u .getId (). equals ( book .getId ())). peek (up ->{
7070 up .setAuthor (book .getAuthor ());
7171 up .setName (book .getName ());
72- return up ;
73- }).collect (Collectors .toList ()).get (0 );
74- return new ResponseEntity <Book >(updatedUser , HttpStatus .CREATED );
72+ }).collect (Collectors .toList ()).get (0 );
73+ return new ResponseEntity <>(updatedUser , HttpStatus .CREATED );
7574 }
7675
7776
0 commit comments