|
32 | 32 | @RequestMapping(value = "/auth/foos") |
33 | 33 | public class FooController { |
34 | 34 |
|
35 | | - @Autowired |
36 | | - private ApplicationEventPublisher eventPublisher; |
37 | | - |
38 | | - @Autowired |
39 | | - private IFooService service; |
40 | | - |
41 | | - public FooController() { |
42 | | - super(); |
43 | | - } |
44 | | - |
45 | | - // API |
46 | | - |
47 | | - @RequestMapping(method = RequestMethod.GET, value = "/count") |
48 | | - @ResponseBody |
49 | | - @ResponseStatus(value = HttpStatus.OK) |
50 | | - public long count() { |
51 | | - return 2l; |
52 | | - } |
53 | | - |
54 | | - // read - one |
55 | | - |
56 | | - @RequestMapping(value = "/{id}", method = RequestMethod.GET) |
57 | | - @ResponseBody |
58 | | - public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) { |
59 | | - final Foo resourceById = RestPreconditions.checkFound(service.findOne(id)); |
60 | | - |
61 | | - eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response)); |
62 | | - return resourceById; |
63 | | - } |
64 | | - |
65 | | - // read - all |
66 | | - |
67 | | - @RequestMapping(method = RequestMethod.GET) |
68 | | - @ResponseBody |
69 | | - public List<Foo> findAll() { |
70 | | - return service.findAll(); |
71 | | - } |
72 | | - |
73 | | - @RequestMapping(params = { "page", "size" }, method = RequestMethod.GET) |
74 | | - @ResponseBody |
75 | | - public List<Foo> findPaginated(@RequestParam("page") final int page, @RequestParam("size") final int size, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) { |
76 | | - final Page<Foo> resultPage = service.findPaginated(page, size); |
77 | | - if (page > resultPage.getTotalPages()) { |
78 | | - throw new MyResourceNotFoundException(); |
79 | | - } |
80 | | - eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent<Foo>(Foo.class, uriBuilder, response, page, resultPage.getTotalPages(), size)); |
81 | | - |
82 | | - return resultPage.getContent(); |
83 | | - } |
84 | | - |
85 | | - // write |
86 | | - |
87 | | - @RequestMapping(method = RequestMethod.POST) |
88 | | - @ResponseStatus(HttpStatus.CREATED) |
89 | | - @ResponseBody |
90 | | - public Foo create(@RequestBody final Foo resource, final HttpServletResponse response) { |
91 | | - Preconditions.checkNotNull(resource); |
92 | | - final Foo foo = service.create(resource); |
93 | | - final Long idOfCreatedResource = foo.getId(); |
94 | | - |
95 | | - eventPublisher.publishEvent(new ResourceCreatedEvent(this, response, idOfCreatedResource)); |
96 | | - |
97 | | - return foo; |
98 | | - } |
99 | | - |
100 | | - @RequestMapping(value = "/{id}", method = RequestMethod.PUT) |
101 | | - @ResponseStatus(HttpStatus.OK) |
102 | | - public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) { |
103 | | - Preconditions.checkNotNull(resource); |
104 | | - RestPreconditions.checkFound(service.findOne(resource.getId())); |
105 | | - service.update(resource); |
106 | | - } |
107 | | - |
108 | | - @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) |
109 | | - @ResponseStatus(HttpStatus.OK) |
110 | | - public void delete(@PathVariable("id") final Long id) { |
111 | | - service.deleteById(id); |
112 | | - } |
113 | | - |
114 | | - @RequestMapping(method = RequestMethod.HEAD) |
115 | | - @ResponseStatus(HttpStatus.OK) |
116 | | - public void head(final HttpServletResponse resp) { |
117 | | - resp.setContentType(MediaType.APPLICATION_JSON_VALUE); |
118 | | - resp.setHeader("bar", "baz"); |
119 | | - } |
| 35 | + @Autowired |
| 36 | + private ApplicationEventPublisher eventPublisher; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private IFooService service; |
| 40 | + |
| 41 | + public FooController() { |
| 42 | + super(); |
| 43 | + } |
| 44 | + |
| 45 | + // API |
| 46 | + |
| 47 | + @RequestMapping(method = RequestMethod.GET, value = "/count") |
| 48 | + @ResponseBody |
| 49 | + @ResponseStatus(value = HttpStatus.OK) |
| 50 | + public long count() { |
| 51 | + return 2l; |
| 52 | + } |
| 53 | + |
| 54 | + // read - one |
| 55 | + |
| 56 | + @RequestMapping(value = "/{id}", method = RequestMethod.GET) |
| 57 | + @ResponseBody |
| 58 | + public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) { |
| 59 | + final Foo resourceById = RestPreconditions.checkFound(service.findOne(id)); |
| 60 | + |
| 61 | + eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response)); |
| 62 | + return resourceById; |
| 63 | + } |
| 64 | + |
| 65 | + // read - all |
| 66 | + |
| 67 | + @RequestMapping(method = RequestMethod.GET) |
| 68 | + @ResponseBody |
| 69 | + public List<Foo> findAll() { |
| 70 | + return service.findAll(); |
| 71 | + } |
| 72 | + |
| 73 | + @RequestMapping(params = { "page", "size" }, method = RequestMethod.GET) |
| 74 | + @ResponseBody |
| 75 | + public List<Foo> findPaginated(@RequestParam("page") final int page, @RequestParam("size") final int size, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) { |
| 76 | + final Page<Foo> resultPage = service.findPaginated(page, size); |
| 77 | + if (page > resultPage.getTotalPages()) { |
| 78 | + throw new MyResourceNotFoundException(); |
| 79 | + } |
| 80 | + eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent<Foo>(Foo.class, uriBuilder, response, page, resultPage.getTotalPages(), size)); |
| 81 | + |
| 82 | + return resultPage.getContent(); |
| 83 | + } |
| 84 | + |
| 85 | + // write |
| 86 | + |
| 87 | + @RequestMapping(method = RequestMethod.POST) |
| 88 | + @ResponseStatus(HttpStatus.CREATED) |
| 89 | + @ResponseBody |
| 90 | + public Foo create(@RequestBody final Foo resource, final HttpServletResponse response) { |
| 91 | + Preconditions.checkNotNull(resource); |
| 92 | + final Foo foo = service.create(resource); |
| 93 | + final Long idOfCreatedResource = foo.getId(); |
| 94 | + |
| 95 | + eventPublisher.publishEvent(new ResourceCreatedEvent(this, response, idOfCreatedResource)); |
| 96 | + |
| 97 | + return foo; |
| 98 | + } |
| 99 | + |
| 100 | + @RequestMapping(value = "/{id}", method = RequestMethod.PUT) |
| 101 | + @ResponseStatus(HttpStatus.OK) |
| 102 | + public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) { |
| 103 | + Preconditions.checkNotNull(resource); |
| 104 | + RestPreconditions.checkFound(service.findOne(resource.getId())); |
| 105 | + service.update(resource); |
| 106 | + } |
| 107 | + |
| 108 | + @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) |
| 109 | + @ResponseStatus(HttpStatus.OK) |
| 110 | + public void delete(@PathVariable("id") final Long id) { |
| 111 | + service.deleteById(id); |
| 112 | + } |
| 113 | + |
| 114 | + @RequestMapping(method = RequestMethod.HEAD) |
| 115 | + @ResponseStatus(HttpStatus.OK) |
| 116 | + public void head(final HttpServletResponse resp) { |
| 117 | + resp.setContentType(MediaType.APPLICATION_JSON_VALUE); |
| 118 | + resp.setHeader("bar", "baz"); |
| 119 | + } |
120 | 120 |
|
121 | 121 | } |
0 commit comments