11package org .baeldung .web .controller ;
22
3+ import static org .springframework .hateoas .mvc .ControllerLinkBuilder .linkTo ;
4+ import static org .springframework .hateoas .mvc .ControllerLinkBuilder .methodOn ;
5+
6+ import java .util .List ;
7+
38import org .baeldung .persistence .model .Customer ;
49import org .baeldung .persistence .model .Order ;
510import org .baeldung .web .service .CustomerService ;
611import org .baeldung .web .service .OrderService ;
712import org .springframework .beans .factory .annotation .Autowired ;
813import org .springframework .hateoas .Link ;
14+ import org .springframework .hateoas .Resources ;
15+ import org .springframework .hateoas .config .EnableHypermediaSupport ;
16+ import org .springframework .hateoas .config .EnableHypermediaSupport .HypermediaType ;
917import org .springframework .web .bind .annotation .PathVariable ;
1018import org .springframework .web .bind .annotation .RequestMapping ;
1119import org .springframework .web .bind .annotation .RequestMethod ;
1220import org .springframework .web .bind .annotation .RestController ;
1321
14- import java .util .List ;
15-
16- import static org .springframework .hateoas .mvc .ControllerLinkBuilder .linkTo ;
17- import static org .springframework .hateoas .mvc .ControllerLinkBuilder .methodOn ;
18-
1922@ RestController
2023@ RequestMapping (value = "/customers" )
24+ @ EnableHypermediaSupport (type = HypermediaType .HAL )
2125public class CustomerController {
2226 @ Autowired
2327 private CustomerService customerService ;
@@ -30,36 +34,41 @@ public Customer getCustomerById(@PathVariable final String customerId) {
3034 return customerService .getCustomerDetail (customerId );
3135 }
3236
33- @ RequestMapping (value = "/{customerId}/{orderId}" , method = RequestMethod . GET )
37+ @ RequestMapping (value = "/{customerId}/{orderId}" )
3438 public Order getOrderById (@ PathVariable final String customerId , @ PathVariable final String orderId ) {
3539 return orderService .getOrderByIdForCustomer (customerId , orderId );
3640 }
3741
38- @ RequestMapping (value = "/{customerId}/orders" , method = RequestMethod .GET )
39- public List <Order > getOrdersForCustomer (@ PathVariable final String customerId ) {
42+ @ RequestMapping (value = "/{customerId}/orders" , method = RequestMethod .GET , produces = { "application/hal+json" } )
43+ public Resources <Order > getOrdersForCustomer (@ PathVariable final String customerId ) {
4044 final List <Order > orders = orderService .getAllOrdersForCustomer (customerId );
4145 for (final Order order : orders ) {
4246 final Link selfLink = linkTo (methodOn (CustomerController .class ).getOrderById (customerId , order .getOrderId ())).withSelfRel ();
4347 order .add (selfLink );
4448 }
45- return orders ;
49+
50+ Link link =linkTo (methodOn (CustomerController .class ).getOrdersForCustomer (customerId )).withSelfRel ();
51+ Resources <Order > result = new Resources <Order >(orders ,link );
52+ return result ;
4653 }
4754
48- @ RequestMapping (method = RequestMethod .GET )
49- public List <Customer > getAllCustomers () {
55+ @ RequestMapping (method = RequestMethod .GET , produces = { "application/hal+json" } )
56+ public Resources <Customer > getAllCustomers () {
5057 final List <Customer > allCustomers = customerService .allCustomers ();
58+
5159 for (final Customer customer : allCustomers ) {
5260 String customerId = customer .getCustomerId ();
5361 Link selfLink = linkTo (CustomerController .class ).slash (customerId ).withSelfRel ();
5462 customer .add (selfLink );
5563 if (orderService .getAllOrdersForCustomer (customerId ).size () > 0 ) {
56- List <Order > methodLinkBuilder = methodOn (CustomerController .class ).getOrdersForCustomer (customerId );
57- final Link ordersLink = linkTo (methodLinkBuilder ).withRel ("allOrders" );
64+ final Link ordersLink = linkTo (methodOn (CustomerController .class ).getOrdersForCustomer (customerId )).withRel ("allOrders" );
5865 customer .add (ordersLink );
5966 }
60-
6167 }
62- return allCustomers ;
68+
69+ Link link =linkTo (CustomerController .class ).withSelfRel ();
70+ Resources <Customer > result = new Resources <Customer >(allCustomers ,link );
71+ return result ;
6372 }
6473
6574}
0 commit comments