Merge pull request #4264 from Doha2012/master

fix spring hateoas
This commit is contained in:
Loredana Crusoveanu 2018-05-17 22:33:18 +03:00 committed by GitHub
commit a9f08084ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 23 deletions

View File

@ -1,23 +1,27 @@
package org.baeldung.web.controller; 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.Customer;
import org.baeldung.persistence.model.Order; import org.baeldung.persistence.model.Order;
import org.baeldung.web.service.CustomerService; import org.baeldung.web.service.CustomerService;
import org.baeldung.web.service.OrderService; import org.baeldung.web.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link; 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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; 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 @RestController
@RequestMapping(value = "/customers") @RequestMapping(value = "/customers")
@EnableHypermediaSupport(type = HypermediaType.HAL)
public class CustomerController { public class CustomerController {
@Autowired @Autowired
private CustomerService customerService; private CustomerService customerService;
@ -35,31 +39,36 @@ public class CustomerController {
return orderService.getOrderByIdForCustomer(customerId, orderId); return orderService.getOrderByIdForCustomer(customerId, orderId);
} }
@RequestMapping(value = "/{customerId}/orders", method = RequestMethod.GET) @RequestMapping(value = "/{customerId}/orders", method = RequestMethod.GET , produces = {"application/hal+json"})
public List<Order> getOrdersForCustomer(@PathVariable final String customerId) { public Resources<Order> getOrdersForCustomer(@PathVariable final String customerId) {
final List<Order> orders = orderService.getAllOrdersForCustomer(customerId); final List<Order> orders = orderService.getAllOrdersForCustomer(customerId);
for (final Order order : orders) { for (final Order order : orders) {
final Link selfLink = linkTo(methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel(); final Link selfLink = linkTo(methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel();
order.add(selfLink); order.add(selfLink);
} }
return orders;
Link link =linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withSelfRel();
Resources<Order> result = new Resources<Order>(orders,link);
return result;
} }
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET, produces = {"application/hal+json"})
public List<Customer> getAllCustomers() { public Resources<Customer> getAllCustomers() {
final List<Customer> allCustomers = customerService.allCustomers(); final List<Customer> allCustomers = customerService.allCustomers();
for (final Customer customer : allCustomers) { for (final Customer customer : allCustomers) {
String customerId = customer.getCustomerId(); String customerId = customer.getCustomerId();
Link selfLink = linkTo(CustomerController.class).slash(customerId).withSelfRel(); Link selfLink = linkTo(CustomerController.class).slash(customerId).withSelfRel();
customer.add(selfLink); customer.add(selfLink);
if (orderService.getAllOrdersForCustomer(customerId).size() > 0) { if (orderService.getAllOrdersForCustomer(customerId).size() > 0) {
List<Order> methodLinkBuilder = methodOn(CustomerController.class).getOrdersForCustomer(customerId); final Link ordersLink = linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withRel("allOrders");
final Link ordersLink = linkTo(methodLinkBuilder).withRel("allOrders");
customer.add(ordersLink); customer.add(ordersLink);
} }
} }
return allCustomers;
Link link =linkTo(CustomerController.class).withSelfRel();
Resources<Customer> result = new Resources<Customer>(allCustomers,link);
return result;
} }
} }

View File

@ -3,6 +3,7 @@ package org.baeldung.web.service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.baeldung.persistence.model.Customer; import org.baeldung.persistence.model.Customer;
import org.baeldung.persistence.model.Order; import org.baeldung.persistence.model.Order;
@ -49,16 +50,9 @@ public class OrderServiceImpl implements OrderService {
@Override @Override
public Order getOrderByIdForCustomer(final String customerId, final String orderId) { public Order getOrderByIdForCustomer(final String customerId, final String orderId) {
final Map<String, Order> orders = customerMap.get(customerId).getOrders();
final List<Order> orders = (List<Order>) customerMap.get(customerId).getOrders().values(); Order selectedOrder = orders.get(orderId);
Order selectedOrder = null;
for (final Order order : orders) {
if (order.getId().equals(orderId)) {
selectedOrder = order;
}
}
return selectedOrder; return selectedOrder;
} }
} }