Spring HATEOAS support

This commit is contained in:
Roshan Thomas 2016-04-06 18:22:46 -04:00
parent 08c8d82cd4
commit 4ec8015949
6 changed files with 254 additions and 0 deletions

View File

@ -76,6 +76,14 @@
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Spring HATEOAS -->
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.19.0.RELEASE</version>
</dependency>
<!-- web -->

View File

@ -0,0 +1,56 @@
package org.baeldung.persistence.model;
import java.util.Map;
import org.springframework.hateoas.ResourceSupport;
public class Customer extends ResourceSupport {
private String customerId;
private String customerName;
private String companyName;
private Map<String, Order> orders;
public Customer() {
super();
}
public Customer(final String customerId, final String customerName, final String companyName) {
super();
this.customerId = customerId;
this.customerName = customerName;
this.companyName = companyName;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(final String customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(final String customerName) {
this.customerName = customerName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(final String companyName) {
this.companyName = companyName;
}
public Map<String, Order> getOrders() {
return orders;
}
public void setOrders(final Map<String, Order> orders) {
this.orders = orders;
}
}

View File

@ -0,0 +1,50 @@
package org.baeldung.persistence.model;
import org.springframework.hateoas.ResourceSupport;
public class Order extends ResourceSupport {
private String orderId;
private double price;
private int quantity;
public Order() {
super();
}
public Order(final String orderId, final double price, final int quantity) {
super();
this.orderId = orderId;
this.price = price;
this.quantity = quantity;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(final String orderId) {
this.orderId = orderId;
}
public double getPrice() {
return price;
}
public void setPrice(final double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(final int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "Order [orderId=" + orderId + ", price=" + price + ", quantity=" + quantity + "]";
}
}

View File

@ -0,0 +1,53 @@
package org.baeldung.web.controller;
import java.util.List;
import org.baeldung.persistence.model.Customer;
import org.baeldung.persistence.model.Order;
import org.baeldung.web.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
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;
@RestController
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET)
public Customer getCustomerById(@PathVariable final String customerId) {
return customerService.getCustomerDetail(customerId);
}
@RequestMapping(value = "/customer/{customerId}/{orderId}", method = RequestMethod.GET)
public Order getOrderByIdForCustomer(@PathVariable final String customerId, @PathVariable final String orderId) {
return customerService.getOrderByIdForCustomer(customerId, orderId);
}
@RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET)
public List<Order> getOrdersForCustomer(@PathVariable final String customerId) {
return customerService.getAllOrdersForCustomer(customerId);
}
@RequestMapping(value = "/customers", method = RequestMethod.GET)
public List<Customer> getAllCustomers() {
final List<Customer> allCustomers = customerService.allCustomers();
for (final Customer customer : allCustomers) {
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel();
customer.add(selfLink);
final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders");
customer.add(ordersLink);
for (final Order order : customer.getOrders().values()) {
final Link orderLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderByIdForCustomer(customer.getCustomerId(), order.getOrderId())).withRel("order");
order.add(orderLink);
}
}
return allCustomers;
}
}

View File

@ -0,0 +1,18 @@
package org.baeldung.web.service;
import java.util.List;
import org.baeldung.persistence.model.Customer;
import org.baeldung.persistence.model.Order;
public interface CustomerService {
List<Customer> allCustomers();
Customer getCustomerDetail(final String id);
List<Order> getAllOrdersForCustomer(String customerId);
Order getOrderByIdForCustomer(String customerId, String orderId);
}

View File

@ -0,0 +1,69 @@
package org.baeldung.web.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.baeldung.persistence.model.Customer;
import org.baeldung.persistence.model.Order;
import org.springframework.stereotype.Service;
@Service
public class CustomerServiceImpl implements CustomerService {
private HashMap<String, Customer> customerMap;
private HashMap<String, Order> customerOneOrderMap;
private HashMap<String, Order> customerTwoOrderMap;
public CustomerServiceImpl() {
customerMap = new HashMap<>();
customerOneOrderMap = new HashMap<>();
customerTwoOrderMap = new HashMap<>();
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
customerOneOrderMap.put("001A", new Order("001A", 150.00, 25));
customerOneOrderMap.put("002A", new Order("002A", 250.00, 15));
customerTwoOrderMap.put("002B", new Order("002B", 550.00, 325));
customerTwoOrderMap.put("002B", new Order("002B", 450.00, 525));
customerOne.setOrders(customerOneOrderMap);
customerTwo.setOrders(customerTwoOrderMap);
customerMap.put("10A", customerOne);
customerMap.put("20B", customerTwo);
}
@Override
public List<Customer> allCustomers() {
return new ArrayList<>(customerMap.values());
}
@Override
public Customer getCustomerDetail(final String customerId) {
return customerMap.get(customerId);
}
@Override
public List<Order> getAllOrdersForCustomer(final String customerId) {
return new ArrayList<>(customerMap.get(customerId).getOrders().values());
}
@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;
}
}
return selectedOrder;
}
}