11package org .baeldung .web .controller ;
22
3+ import java .net .URI ;
4+
5+ import javax .servlet .http .HttpServletRequest ;
36import javax .servlet .http .HttpServletResponse ;
47
8+ import org .baeldung .persistence .service .FooService ;
59import org .baeldung .web .dto .Foo ;
10+ import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .context .ApplicationEventPublisher ;
12+ import org .springframework .http .HttpStatus ;
613import org .springframework .stereotype .Controller ;
714import org .springframework .web .bind .annotation .PathVariable ;
15+ import org .springframework .web .bind .annotation .RequestBody ;
816import org .springframework .web .bind .annotation .RequestMapping ;
917import org .springframework .web .bind .annotation .RequestMethod ;
1018import org .springframework .web .bind .annotation .ResponseBody ;
19+ import org .springframework .web .bind .annotation .ResponseStatus ;
1120import org .springframework .web .util .UriComponentsBuilder ;
21+ import org .springframework .web .util .UriTemplate ;
22+
23+ import com .google .common .base .Preconditions ;
1224
1325@ Controller
1426@ RequestMapping (value = "/foo" )
1527public class FooController {
1628
29+ @ Autowired
30+ private ApplicationEventPublisher eventPublisher ;
31+
32+ @ Autowired
33+ private FooService service ;
34+
1735 public FooController () {
1836 super ();
1937 }
@@ -26,4 +44,31 @@ public Foo findOne(@PathVariable("id") final Long id, final UriComponentsBuilder
2644 return new Foo ();
2745 }
2846
47+ @ RequestMapping (value = "admin/foo/{id}" , method = RequestMethod .GET )
48+ @ ResponseBody
49+ public Foo get (@ PathVariable ("id" ) final Long id , final HttpServletRequest request , final HttpServletResponse response ) {
50+ final Foo resourceById = Preconditions .checkNotNull (service .getById (id ));
51+
52+ eventPublisher .publishEvent (new SingleResourceRetrieved (this , request , response ));
53+ return resourceById ;
54+ }
55+
56+ @ RequestMapping (value = "admin/foo" , method = RequestMethod .POST )
57+ @ ResponseStatus (HttpStatus .CREATED )
58+ public void create (@ RequestBody final Foo resource , final HttpServletRequest request , final HttpServletResponse response ) {
59+ Preconditions .checkNotNull (resource );
60+ final Long idOfCreatedResource = service .create (resource );
61+
62+ eventPublisher .publishEvent (new ResourceCreated (this , request , response , idOfCreatedResource ));
63+ }
64+
65+ @ RequestMapping (value = "admin" , method = RequestMethod .GET )
66+ @ ResponseStatus (value = HttpStatus .NO_CONTENT )
67+ public void adminRoot (final HttpServletRequest request , final HttpServletResponse response ) {
68+ final String rootUri = request .getRequestURL ().toString ();
69+
70+ final URI fooUri = new UriTemplate ("{rootUri}/{resource}" ).expand (rootUri , "foo" );
71+ final String linkToFoo = LinkUtil .createLinkHeader (fooUri .toASCIIString (), "collection" );
72+ response .addHeader ("Link" , linkToFoo );
73+ }
2974}
0 commit comments