Skip to content

Commit bd9f672

Browse files
committed
fix spring hateoas
1 parent 2d54c25 commit bd9f672

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package 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+
38
import org.baeldung.persistence.model.Customer;
49
import org.baeldung.persistence.model.Order;
510
import org.baeldung.web.service.CustomerService;
611
import org.baeldung.web.service.OrderService;
712
import org.springframework.beans.factory.annotation.Autowired;
813
import 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;
917
import org.springframework.web.bind.annotation.PathVariable;
1018
import org.springframework.web.bind.annotation.RequestMapping;
1119
import org.springframework.web.bind.annotation.RequestMethod;
1220
import 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)
2125
public 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
}

‎spring-security-rest/src/main/java/org/baeldung/web/service/OrderServiceImpl.java‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.HashMap;
55
import java.util.List;
6+
import java.util.Map;
67

78
import org.baeldung.persistence.model.Customer;
89
import org.baeldung.persistence.model.Order;
@@ -49,16 +50,9 @@ public List<Order> getAllOrdersForCustomer(final String customerId) {
4950

5051
@Override
5152
public Order getOrderByIdForCustomer(final String customerId, final String orderId) {
52-
53-
final List<Order> orders = (List<Order>) customerMap.get(customerId).getOrders().values();
54-
Order selectedOrder = null;
55-
for (final Order order : orders) {
56-
if (order.getId().equals(orderId)) {
57-
selectedOrder = order;
58-
}
59-
}
53+
final Map<String, Order> orders = customerMap.get(customerId).getOrders();
54+
Order selectedOrder = orders.get(orderId);
6055
return selectedOrder;
61-
6256
}
6357

6458
}

0 commit comments

Comments
 (0)