|
1 | | -package com.example; |
| 1 | +package com.baeldung.restdocs; |
2 | 2 |
|
3 | 3 | import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; |
4 | 4 |
|
|
7 | 7 |
|
8 | 8 | import org.springframework.http.HttpHeaders; |
9 | 9 | import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 11 | +import org.springframework.web.bind.annotation.GetMapping; |
| 12 | +import org.springframework.web.bind.annotation.PatchMapping; |
10 | 13 | import org.springframework.web.bind.annotation.PathVariable; |
| 14 | +import org.springframework.web.bind.annotation.PostMapping; |
| 15 | +import org.springframework.web.bind.annotation.PutMapping; |
11 | 16 | import org.springframework.web.bind.annotation.RequestBody; |
12 | 17 | import org.springframework.web.bind.annotation.RequestMapping; |
13 | | -import org.springframework.web.bind.annotation.RequestMethod; |
14 | 18 | import org.springframework.web.bind.annotation.ResponseStatus; |
15 | 19 | import org.springframework.web.bind.annotation.RestController; |
16 | 20 |
|
17 | 21 | @RestController |
18 | 22 | @RequestMapping("/crud") |
19 | 23 | public class CRUDController { |
20 | 24 |
|
21 | | - @RequestMapping(method = RequestMethod.GET) |
22 | | - @ResponseStatus(HttpStatus.OK) |
| 25 | + @GetMapping |
23 | 26 | public List<CrudInput> read(@RequestBody CrudInput crudInput) { |
24 | 27 | List<CrudInput> returnList = new ArrayList<CrudInput>(); |
25 | 28 | returnList.add(crudInput); |
26 | 29 | return returnList; |
27 | 30 | } |
28 | 31 |
|
29 | 32 | @ResponseStatus(HttpStatus.CREATED) |
30 | | - @RequestMapping(method = RequestMethod.POST) |
| 33 | + @PostMapping |
31 | 34 | public HttpHeaders save(@RequestBody CrudInput crudInput) { |
32 | 35 | HttpHeaders httpHeaders = new HttpHeaders(); |
33 | | - httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getTitle()).toUri()); |
| 36 | + httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getId()).toUri()); |
34 | 37 | return httpHeaders; |
35 | 38 | } |
36 | 39 |
|
37 | | - @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) |
| 40 | + @DeleteMapping(value = "/{id}") |
38 | 41 | @ResponseStatus(HttpStatus.OK) |
39 | | - HttpHeaders delete(@RequestBody CrudInput crudInput) { |
40 | | - HttpHeaders httpHeaders = new HttpHeaders(); |
41 | | - return httpHeaders; |
| 42 | + HttpHeaders delete(@PathVariable("id") long id) { |
| 43 | + return new HttpHeaders(); |
42 | 44 | } |
43 | 45 |
|
44 | | - @RequestMapping(value = "/{id}", method = RequestMethod.PUT) |
| 46 | + @PutMapping(value = "/{id}") |
45 | 47 | @ResponseStatus(HttpStatus.ACCEPTED) |
46 | 48 | void put(@PathVariable("id") long id, @RequestBody CrudInput crudInput) { |
47 | 49 |
|
48 | 50 | } |
49 | 51 |
|
50 | | - @RequestMapping(value = "/{id}", method = RequestMethod.PATCH) |
51 | | - @ResponseStatus(HttpStatus.NO_CONTENT) |
52 | | - void patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) { |
53 | | - |
| 52 | + @PatchMapping(value = "/{id}") |
| 53 | + public List<CrudInput> patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) { |
| 54 | + List<CrudInput> returnList = new ArrayList<CrudInput>(); |
| 55 | + crudInput.setId(id); |
| 56 | + returnList.add(crudInput); |
| 57 | + return returnList; |
54 | 58 | } |
55 | 59 | } |
0 commit comments