3
3
import lombok .RequiredArgsConstructor ;
4
4
import org .springframework .beans .factory .annotation .Autowired ;
5
5
import org .springframework .data .rest .webmvc .RepositoryLinksResource ;
6
- import org .springframework .hateoas .*;
6
+ import org .springframework .hateoas .CollectionModel ;
7
+ import org .springframework .hateoas .Link ;
8
+ import org .springframework .hateoas .RepresentationModel ;
7
9
import org .springframework .hateoas .server .ExposesResourceFor ;
8
10
import org .springframework .hateoas .server .RepresentationModelProcessor ;
9
- import org .springframework .hateoas .server .mvc .ControllerLinkBuilder ;
11
+ import org .springframework .hateoas .server .mvc .WebMvcLinkBuilder ;
10
12
import org .springframework .http .HttpStatus ;
11
13
import org .springframework .http .ResponseEntity ;
12
14
import org .springframework .web .bind .annotation .*;
@@ -34,22 +36,22 @@ public RepresentationModel<Expense> getExpenseRoot() {
34
36
}
35
37
36
38
private Link getLinkToExpenses (String rel , DateWrapper date ) {
37
- return ControllerLinkBuilder .linkTo (methodOn (ExpenseEndpoint .class ).getExpensesOfMonth (date .getYear (), date .getMonth ())).withRel (rel );
39
+ return WebMvcLinkBuilder .linkTo (methodOn (ExpenseEndpoint .class ).getExpensesOfMonth (date .getYear (), date .getMonth ())).withRel (rel );
38
40
}
39
41
40
42
@ RequestMapping (method = RequestMethod .POST )
41
43
public ResponseEntity <?> addExpense (@ RequestBody ExpenseCreationDTO expenseCreationDTO ) {
42
44
DateWrapper date = new DateWrapper ();
43
45
Expense expense = expenseService .save (expenseCreationDTO .getAmount (), expenseCreationDTO .getComment (), date .getYear (), date .getMonth ());
44
- URI location = ControllerLinkBuilder .linkTo (methodOn (ExpenseEndpoint .class ).getExpense (date .getYear (), date .getMonth (), expense .getNumericId ())).toUri ();
46
+ URI location = WebMvcLinkBuilder .linkTo (methodOn (ExpenseEndpoint .class ).getExpense (date .getYear (), date .getMonth (), expense .getNumericId ())).toUri ();
45
47
return ResponseEntity .created (location ).build ();
46
48
}
47
49
48
50
@ RequestMapping (method = RequestMethod .GET , value = "/{year}/{month}" )
49
51
public ResponseEntity <CollectionModel <Expense >> getExpensesOfMonth (@ PathVariable ("year" ) Integer year , @ PathVariable ("month" ) Integer month ) {
50
52
Collection <Expense > byYearAndMonth = expenseService .find (new DateWrapper (year , month ));
51
53
byYearAndMonth .forEach (this ::addSelfLink );
52
- CollectionModel <Expense > expenseResource = new CollectionModel <> (byYearAndMonth );
54
+ CollectionModel <Expense > expenseResource = CollectionModel . of (byYearAndMonth );
53
55
DateWrapper date = new DateWrapper (year , month );
54
56
expenseResource .add (getLinkToExpenses ("self" , date ));
55
57
expenseResource .add (getLinkToExpenses ("previous" , date .getPreviousMonth ()));
@@ -59,8 +61,8 @@ public ResponseEntity<CollectionModel<Expense>> getExpensesOfMonth(@PathVariable
59
61
60
62
private void addSelfLink (Expense expense ) {
61
63
expense .add (linkTo (methodOn (ExpenseEndpoint .class )
62
- .getExpense (expense .getYear (), expense .getMonth (), expense .getNumericId ()))
63
- .withSelfRel ());
64
+ .getExpense (expense .getYear (), expense .getMonth (), expense .getNumericId ()))
65
+ .withSelfRel ());
64
66
}
65
67
66
68
@ RequestMapping (method = RequestMethod .POST , value = "/{year}/{month}" )
@@ -69,7 +71,7 @@ public ResponseEntity<?> addExpenseInMonth(@PathVariable("year") Integer year,
69
71
@ PathVariable ("month" ) Integer month ,
70
72
@ RequestBody ExpenseCreationDTO expenseCreationDTO ) throws URISyntaxException {
71
73
Expense expense = expenseService .save (expenseCreationDTO .getAmount (), expenseCreationDTO .getComment (), year , month );
72
- URI location = ControllerLinkBuilder .linkTo (methodOn (ExpenseEndpoint .class ).getExpense (year , month , expense .getNumericId ())).toUri ();
74
+ URI location = WebMvcLinkBuilder .linkTo (methodOn (ExpenseEndpoint .class ).getExpense (year , month , expense .getNumericId ())).toUri ();
73
75
return ResponseEntity .created (location ).build ();
74
76
}
75
77
@@ -95,7 +97,7 @@ public ResponseEntity<?> deleteExpense(@PathVariable("year") Integer year,
95
97
96
98
@ Override
97
99
public RepositoryLinksResource process (RepositoryLinksResource resource ) {
98
- resource .add (ControllerLinkBuilder .linkTo (ExpenseEndpoint .class ).withRel ("expenses" ));
100
+ resource .add (WebMvcLinkBuilder .linkTo (ExpenseEndpoint .class ).withRel ("expenses" ));
99
101
return resource ;
100
102
}
101
103
}
0 commit comments