From bd9f672239c4e68be8a67fc7183bdad1dc6dee71 Mon Sep 17 00:00:00 2001 From: DOHA Date: Thu, 17 May 2018 13:12:44 +0200 Subject: [PATCH] fix spring hateoas --- .../web/controller/CustomerController.java | 39 ++++++++++++------- .../web/service/OrderServiceImpl.java | 12 ++---- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java index 532e6b4d3a..1ca82b8483 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java @@ -1,23 +1,27 @@ package org.baeldung.web.controller; +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; + +import java.util.List; + import org.baeldung.persistence.model.Customer; import org.baeldung.persistence.model.Order; import org.baeldung.web.service.CustomerService; import org.baeldung.web.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.Link; +import org.springframework.hateoas.Resources; +import org.springframework.hateoas.config.EnableHypermediaSupport; +import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import java.util.List; - -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; - @RestController @RequestMapping(value = "/customers") +@EnableHypermediaSupport(type = HypermediaType.HAL) public class CustomerController { @Autowired private CustomerService customerService; @@ -30,36 +34,41 @@ public class CustomerController { return customerService.getCustomerDetail(customerId); } - @RequestMapping(value = "/{customerId}/{orderId}", method = RequestMethod.GET) + @RequestMapping(value = "/{customerId}/{orderId}") public Order getOrderById(@PathVariable final String customerId, @PathVariable final String orderId) { return orderService.getOrderByIdForCustomer(customerId, orderId); } - @RequestMapping(value = "/{customerId}/orders", method = RequestMethod.GET) - public List getOrdersForCustomer(@PathVariable final String customerId) { + @RequestMapping(value = "/{customerId}/orders", method = RequestMethod.GET , produces = {"application/hal+json"}) + public Resources getOrdersForCustomer(@PathVariable final String customerId) { final List orders = orderService.getAllOrdersForCustomer(customerId); for (final Order order : orders) { final Link selfLink = linkTo(methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel(); order.add(selfLink); } - return orders; + + Link link =linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withSelfRel(); + Resources result = new Resources(orders,link); + return result; } - @RequestMapping(method = RequestMethod.GET) - public List getAllCustomers() { + @RequestMapping(method = RequestMethod.GET, produces = {"application/hal+json"}) + public Resources getAllCustomers() { final List allCustomers = customerService.allCustomers(); + for (final Customer customer : allCustomers) { String customerId = customer.getCustomerId(); Link selfLink = linkTo(CustomerController.class).slash(customerId).withSelfRel(); customer.add(selfLink); if (orderService.getAllOrdersForCustomer(customerId).size() > 0) { - List methodLinkBuilder = methodOn(CustomerController.class).getOrdersForCustomer(customerId); - final Link ordersLink = linkTo(methodLinkBuilder).withRel("allOrders"); + final Link ordersLink = linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withRel("allOrders"); customer.add(ordersLink); } - } - return allCustomers; + + Link link =linkTo(CustomerController.class).withSelfRel(); + Resources result = new Resources(allCustomers,link); + return result; } } diff --git a/spring-security-rest/src/main/java/org/baeldung/web/service/OrderServiceImpl.java b/spring-security-rest/src/main/java/org/baeldung/web/service/OrderServiceImpl.java index 1b8e7b0dde..0a6d4708a1 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/service/OrderServiceImpl.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/service/OrderServiceImpl.java @@ -3,6 +3,7 @@ package org.baeldung.web.service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import org.baeldung.persistence.model.Customer; import org.baeldung.persistence.model.Order; @@ -49,16 +50,9 @@ public class OrderServiceImpl implements OrderService { @Override public Order getOrderByIdForCustomer(final String customerId, final String orderId) { - - final List orders = (List) customerMap.get(customerId).getOrders().values(); - Order selectedOrder = null; - for (final Order order : orders) { - if (order.getId().equals(orderId)) { - selectedOrder = order; - } - } + final Map orders = customerMap.get(customerId).getOrders(); + Order selectedOrder = orders.get(orderId); return selectedOrder; - } }