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;
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;
@ -35,31 +39,36 @@ public class CustomerController {
return orderService.getOrderByIdForCustomer(customerId, orderId);
}
@RequestMapping(value = "/{customerId}/orders", method = RequestMethod.GET)
public List<Order> getOrdersForCustomer(@PathVariable final String customerId) {
@RequestMapping(value = "/{customerId}/orders", method = RequestMethod.GET , produces = {"application/hal+json"})
public Resources<Order> getOrdersForCustomer(@PathVariable final String customerId) {
final List<Order> 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<Order> result = new Resources<Order>(orders,link);
return result;
}
@RequestMapping(method = RequestMethod.GET)
public List<Customer> getAllCustomers() {
@RequestMapping(method = RequestMethod.GET, produces = {"application/hal+json"})
public Resources<Customer> getAllCustomers() {
final List<Customer> 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<Order> 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<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.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<Order> orders = (List<Order>) customerMap.get(customerId).getOrders().values();
Order selectedOrder = null;
for (final Order order : orders) {
if (order.getId().equals(orderId)) {
selectedOrder = order;
}
}
final Map<String, Order> orders = customerMap.get(customerId).getOrders();
Order selectedOrder = orders.get(orderId);
return selectedOrder;
}
}