1+ package org .baeldung .web .controller ;
2+
3+ import static org .apache .commons .lang3 .RandomStringUtils .randomAlphabetic ;
4+
5+ import java .util .Arrays ;
6+ import java .util .List ;
7+
8+ import javax .servlet .http .HttpServletResponse ;
9+
10+ import org .baeldung .web .dto .Foo ;
11+ import org .springframework .beans .factory .annotation .Autowired ;
12+ import org .springframework .context .ApplicationEventPublisher ;
13+ import org .springframework .http .HttpStatus ;
14+ import org .springframework .stereotype .Controller ;
15+ import org .springframework .web .bind .annotation .PathVariable ;
16+ import org .springframework .web .bind .annotation .RequestBody ;
17+ import org .springframework .web .bind .annotation .RequestMapping ;
18+ import org .springframework .web .bind .annotation .RequestMethod ;
19+ import org .springframework .web .bind .annotation .ResponseBody ;
20+ import org .springframework .web .bind .annotation .ResponseStatus ;
21+ import org .springframework .web .util .UriComponentsBuilder ;
22+
23+ @ Controller
24+ @ RequestMapping (value = "/auth/foos" )
25+ public class FooController {
26+
27+ @ Autowired
28+ private ApplicationEventPublisher eventPublisher ;
29+
30+ public FooController () {
31+ super ();
32+ }
33+
34+ // API
35+
36+ // read - single
37+
38+ @ RequestMapping (value = "/{id}" , method = RequestMethod .GET )
39+ @ ResponseBody
40+ public Foo findById (@ PathVariable ("id" ) final Long id , final UriComponentsBuilder uriBuilder , final HttpServletResponse response ) {
41+ return new Foo (randomAlphabetic (6 ));
42+ }
43+
44+ // read - multiple
45+
46+ @ RequestMapping (method = RequestMethod .GET )
47+ @ ResponseBody
48+ public List <Foo > findAll () {
49+ return Arrays .asList (new Foo (randomAlphabetic (6 )));
50+ }
51+
52+ // write - just for test
53+ @ RequestMapping (method = RequestMethod .POST )
54+ @ ResponseStatus (HttpStatus .CREATED )
55+ @ ResponseBody
56+ public Foo create (@ RequestBody final Foo foo ) {
57+ return foo ;
58+ }
59+ }
0 commit comments