11package com .baeldung .web ;
22
3- import java .util .List ;
4- import java .util .Optional ;
5-
6- import javax .validation .constraints .Max ;
7- import javax .validation .constraints .Min ;
8-
3+ import com .baeldung .persistence .FooRepository ;
94import org .springframework .beans .factory .annotation .Autowired ;
10- import org .springframework .data .domain .Page ;
115import org .springframework .data .domain .PageRequest ;
126import org .springframework .http .HttpStatus ;
137import org .springframework .validation .annotation .Validated ;
14- import org .springframework .web .bind .annotation .PathVariable ;
15- import org .springframework .web .bind .annotation .RequestBody ;
16- import org .springframework .web .bind .annotation .RequestMapping ;
17- import org .springframework .web .bind .annotation .RequestMethod ;
18- import org .springframework .web .bind .annotation .RequestParam ;
19- import org .springframework .web .bind .annotation .ResponseBody ;
20- import org .springframework .web .bind .annotation .ResponseStatus ;
21- import org .springframework .web .bind .annotation .RestController ;
8+ import org .springframework .web .bind .annotation .*;
229
23- import com .baeldung .persistence .FooRepository ;
10+ import javax .validation .constraints .Max ;
11+ import javax .validation .constraints .Min ;
12+ import java .util .List ;
2413
2514@ RestController ("/foos" )
2615public class FooController {
@@ -30,30 +19,29 @@ public class FooController {
3019
3120 // API - read
3221
33- @ RequestMapping ( method = RequestMethod . GET , value = "/foos/{id}" )
22+ @ GetMapping ( "/foos/{id}" )
3423 @ ResponseBody
3524 @ Validated
3625 public Foo findById (@ PathVariable @ Min (0 ) final long id ) {
37- return repo .findOne (id ).orElse (null );
26+ return repo .findById (id ).orElse (null );
3827 }
3928
40- @ RequestMapping ( method = RequestMethod . GET )
29+ @ GetMapping
4130 @ ResponseBody
4231 public List <Foo > findAll () {
4332 return repo .findAll ();
4433 }
4534
46- @ RequestMapping (params = { "page" , "size" }, method = RequestMethod . GET )
35+ @ GetMapping (params = { "page" , "size" })
4736 @ ResponseBody
4837 @ Validated
4938 public List <Foo > findPaginated (@ RequestParam ("page" ) @ Min (0 ) final int page , @ Max (100 ) @ RequestParam ("size" ) final int size ) {
50- final Page <Foo > resultPage = repo .findAll (new PageRequest (page , size ));
51- return resultPage .getContent ();
39+ return repo .findAll (PageRequest .of (page , size )).getContent ();
5240 }
5341
5442 // API - write
5543
56- @ RequestMapping ( method = RequestMethod . PUT , value = "/foos/{id}" )
44+ @ PutMapping ( "/foos/{id}" )
5745 @ ResponseStatus (HttpStatus .OK )
5846 @ ResponseBody
5947 public Foo update (@ PathVariable ("id" ) final String id , @ RequestBody final Foo foo ) {
0 commit comments