|
1 | 1 | package org.baeldung.web.controller; |
2 | 2 |
|
3 | | -import java.util.List; |
4 | | - |
5 | 3 | import org.baeldung.persistence.model.Customer; |
6 | 4 | import org.baeldung.persistence.model.Order; |
7 | 5 | import org.baeldung.web.service.CustomerService; |
8 | 6 | import org.baeldung.web.service.OrderService; |
9 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
10 | 8 | import org.springframework.hateoas.Link; |
11 | | -import org.springframework.hateoas.mvc.ControllerLinkBuilder; |
12 | 9 | import org.springframework.web.bind.annotation.PathVariable; |
13 | 10 | import org.springframework.web.bind.annotation.RequestMapping; |
14 | 11 | import org.springframework.web.bind.annotation.RequestMethod; |
15 | 12 | import org.springframework.web.bind.annotation.RestController; |
16 | 13 |
|
| 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 | + |
17 | 19 | @RestController |
| 20 | +@RequestMapping(value = "/customer") |
18 | 21 | public class CustomerController { |
19 | 22 | @Autowired |
20 | 23 | private CustomerService customerService; |
21 | 24 |
|
22 | 25 | @Autowired |
23 | 26 | private OrderService orderService; |
24 | 27 |
|
25 | | - @RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET) |
| 28 | + @RequestMapping(value = "/{customerId}", method = RequestMethod.GET) |
26 | 29 | public Customer getCustomerById(@PathVariable final String customerId) { |
27 | 30 | return customerService.getCustomerDetail(customerId); |
28 | 31 | } |
29 | 32 |
|
30 | | - @RequestMapping(value = "/customer/{customerId}/{orderId}", method = RequestMethod.GET) |
| 33 | + @RequestMapping(value = "/{customerId}/{orderId}", method = RequestMethod.GET) |
31 | 34 | public Order getOrderById(@PathVariable final String customerId, @PathVariable final String orderId) { |
32 | 35 | return orderService.getOrderByIdForCustomer(customerId, orderId); |
33 | 36 | } |
34 | 37 |
|
35 | | - @RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET) |
| 38 | + @RequestMapping(value = "/{customerId}/orders", method = RequestMethod.GET) |
36 | 39 | public List<Order> getOrdersForCustomer(@PathVariable final String customerId) { |
37 | 40 | final List<Order> orders = orderService.getAllOrdersForCustomer(customerId); |
38 | 41 | for (final Order order : orders) { |
39 | | - final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel(); |
| 42 | + final Link selfLink = linkTo(methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel(); |
40 | 43 | order.add(selfLink); |
41 | 44 | } |
42 | 45 | return orders; |
43 | 46 | } |
44 | 47 |
|
45 | | - @RequestMapping(value = "/customers", method = RequestMethod.GET) |
| 48 | + @RequestMapping(method = RequestMethod.GET) |
46 | 49 | public List<Customer> getAllCustomers() { |
47 | 50 | final List<Customer> allCustomers = customerService.allCustomers(); |
48 | 51 | for (final Customer customer : allCustomers) { |
49 | | - final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel(); |
| 52 | + String customerId = customer.getCustomerId(); |
| 53 | + Link selfLink = linkTo(CustomerController.class).slash(customerId).withSelfRel(); |
50 | 54 | customer.add(selfLink); |
51 | | - if (orderService.getAllOrdersForCustomer(customer.getCustomerId()).size() > 0) { |
52 | | - final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders"); |
| 55 | + if (orderService.getAllOrdersForCustomer(customerId).size() > 0) { |
| 56 | + List<Order> methodLinkBuilder = methodOn(CustomerController.class).getOrdersForCustomer(customerId); |
| 57 | + final Link ordersLink = linkTo(methodLinkBuilder).withRel("allOrders"); |
53 | 58 | customer.add(ordersLink); |
54 | 59 | } |
55 | 60 |
|
|
0 commit comments