1- package com .baeldung .web ;
2-
3- import java .util .List ;
4-
5- import javax .validation .constraints .Max ;
6- import javax .validation .constraints .Min ;
7-
8- import org .springframework .beans .factory .annotation .Autowired ;
9- import org .springframework .data .domain .PageRequest ;
10- import org .springframework .http .HttpStatus ;
11- import org .springframework .validation .annotation .Validated ;
12- import org .springframework .web .bind .annotation .GetMapping ;
13- import org .springframework .web .bind .annotation .PathVariable ;
14- import org .springframework .web .bind .annotation .PostMapping ;
15- import org .springframework .web .bind .annotation .PutMapping ;
16- import org .springframework .web .bind .annotation .RequestBody ;
17- import org .springframework .web .bind .annotation .RequestParam ;
18- import org .springframework .web .bind .annotation .ResponseStatus ;
19- import org .springframework .web .bind .annotation .RestController ;
20- import org .springframework .web .server .ResponseStatusException ;
21-
22- import com .baeldung .model .Foo ;
23- import com .baeldung .persistence .FooRepository ;
24-
25- @ RestController ( "/foos" )
26- public class FooController {
27-
28- @ Autowired
29- private FooRepository repo ;
30-
31- // API - read
32-
33- @ GetMapping ("/foos/{id}" )
34- @ Validated
35- public Foo findById (@ PathVariable @ Min (0 ) final long id ) {
36- return repo .findById (id ).orElse (null );
37- }
38-
39- @ GetMapping
40- public List <Foo > findAll () {
41- return repo .findAll ();
42- }
43-
44- @ GetMapping (params = { "page" , "size" })
45- @ Validated
46- public List <Foo > findPaginated (@ RequestParam ("page" ) @ Min (0 ) final int page , @ Max (100 ) @ RequestParam ("size" ) final int size ) {
47- return repo .findAll (PageRequest .of (page , size )).getContent ();
48- }
49-
50- // API - write
51-
52- @ PutMapping ("/foos/{id}" )
53- @ ResponseStatus (HttpStatus .OK )
54- public Foo update (@ PathVariable ("id" ) final String id , @ RequestBody final Foo foo ) {
55- return foo ;
56- }
57-
58- @ PostMapping
59- @ ResponseStatus (HttpStatus .CREATED )
60- public void create ( @ RequestBody final Foo foo ) {
61- if (null == foo || null == foo .getName ()) {
62- throw new ResponseStatusException (HttpStatus .BAD_REQUEST ," 'name' is required" );
63- }
64- repo .save (foo );
65- }
66- }
1+ package com .baeldung .web ;
2+
3+ import java .util .List ;
4+
5+ import javax .validation .constraints .Max ;
6+ import javax .validation .constraints .Min ;
7+
8+ import org .springframework .beans .factory .annotation .Autowired ;
9+ import org .springframework .data .domain .PageRequest ;
10+ import org .springframework .http .HttpStatus ;
11+ import org .springframework .validation .annotation .Validated ;
12+ import org .springframework .web .bind .annotation .GetMapping ;
13+ import org .springframework .web .bind .annotation .PathVariable ;
14+ import org .springframework .web .bind .annotation .PostMapping ;
15+ import org .springframework .web .bind .annotation .PutMapping ;
16+ import org .springframework .web .bind .annotation .RequestBody ;
17+ import org .springframework .web .bind .annotation .RequestParam ;
18+ import org .springframework .web .bind .annotation .ResponseStatus ;
19+ import org .springframework .web .bind .annotation .RestController ;
20+ import org .springframework .web .server .ResponseStatusException ;
21+
22+ import com .baeldung .model .Foo ;
23+ import com .baeldung .persistence .FooRepository ;
24+
25+ @ RestController
26+ public class FooController {
27+
28+ @ Autowired
29+ private FooRepository repo ;
30+
31+ // API - read
32+
33+ @ GetMapping ("/foos/{id}" )
34+ @ Validated
35+ public Foo findById (@ PathVariable @ Min (0 ) final long id ) {
36+ return repo .findById (id ).orElse (null );
37+ }
38+
39+ @ GetMapping ( "/foos" )
40+ public List <Foo > findAll () {
41+ return repo .findAll ();
42+ }
43+
44+ @ GetMapping ( value = "/foos" , params = { "page" , "size" })
45+ @ Validated
46+ public List <Foo > findPaginated (@ RequestParam ("page" ) @ Min (0 ) final int page , @ Max (100 ) @ RequestParam ("size" ) final int size ) {
47+ return repo .findAll (PageRequest .of (page , size )).getContent ();
48+ }
49+
50+ // API - write
51+
52+ @ PutMapping ("/foos/{id}" )
53+ @ ResponseStatus (HttpStatus .OK )
54+ public Foo update (@ PathVariable ("id" ) final String id , @ RequestBody final Foo foo ) {
55+ return foo ;
56+ }
57+
58+ @ PostMapping ( "/foos" )
59+ @ ResponseStatus (HttpStatus .CREATED )
60+ public void create ( @ RequestBody final Foo foo ) {
61+ if (null == foo || null == foo .getName ()) {
62+ throw new ResponseStatusException (HttpStatus .BAD_REQUEST ," 'name' is required" );
63+ }
64+ repo .save (foo );
65+ }
66+ }
0 commit comments