Merge pull request #402 from roshanathomas/master
Spring HATEOAS support
This commit is contained in:
commit
398b0e30f6
|
@ -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 -->
|
||||
|
||||
|
@ -329,4 +337,4 @@
|
|||
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -43,6 +43,8 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
|
|||
.and()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/api/csrfAttacker*").permitAll()
|
||||
.antMatchers("/api/customers**").permitAll()
|
||||
.antMatchers("/api/customer/**").permitAll()
|
||||
.antMatchers("/api/**").authenticated()
|
||||
.and()
|
||||
.formLogin()
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
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.baeldung.web.service.OrderService;
|
||||
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;
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@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 getOrderById(@PathVariable final String customerId, @PathVariable final String orderId) {
|
||||
return orderService.getOrderByIdForCustomer(customerId, orderId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET)
|
||||
public List<Order> getOrdersForCustomer(@PathVariable final String customerId) {
|
||||
final List<Order> orders = orderService.getAllOrdersForCustomer(customerId);
|
||||
for (final Order order : orders) {
|
||||
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel();
|
||||
order.add(selfLink);
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
@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);
|
||||
if (orderService.getAllOrdersForCustomer(customer.getCustomerId()).size() > 0) {
|
||||
final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders");
|
||||
customer.add(ordersLink);
|
||||
}
|
||||
|
||||
}
|
||||
return allCustomers;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package org.baeldung.web.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Customer;
|
||||
|
||||
public interface CustomerService {
|
||||
|
||||
List<Customer> allCustomers();
|
||||
|
||||
Customer getCustomerDetail(final String id);
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
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.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CustomerServiceImpl implements CustomerService {
|
||||
|
||||
private HashMap<String, Customer> customerMap;
|
||||
|
||||
public CustomerServiceImpl() {
|
||||
|
||||
customerMap = new HashMap<>();
|
||||
|
||||
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
|
||||
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
|
||||
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
|
||||
|
||||
customerMap.put("10A", customerOne);
|
||||
customerMap.put("20B", customerTwo);
|
||||
customerMap.put("30C", customerThree);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Customer> allCustomers() {
|
||||
return new ArrayList<>(customerMap.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getCustomerDetail(final String customerId) {
|
||||
return customerMap.get(customerId);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package org.baeldung.web.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Order;
|
||||
|
||||
public interface OrderService {
|
||||
|
||||
List<Order> getAllOrdersForCustomer(String customerId);
|
||||
|
||||
Order getOrderByIdForCustomer(String customerId, String orderId);
|
||||
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
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 OrderServiceImpl implements OrderService {
|
||||
|
||||
private HashMap<String, Customer> customerMap;
|
||||
private HashMap<String, Order> customerOneOrderMap;
|
||||
private HashMap<String, Order> customerTwoOrderMap;
|
||||
private HashMap<String, Order> customerThreeOrderMap;
|
||||
|
||||
public OrderServiceImpl() {
|
||||
|
||||
customerMap = new HashMap<>();
|
||||
customerOneOrderMap = new HashMap<>();
|
||||
customerTwoOrderMap = new HashMap<>();
|
||||
customerThreeOrderMap = new HashMap<>();
|
||||
|
||||
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));
|
||||
|
||||
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
|
||||
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
|
||||
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
|
||||
|
||||
customerOne.setOrders(customerOneOrderMap);
|
||||
customerTwo.setOrders(customerTwoOrderMap);
|
||||
customerThree.setOrders(customerThreeOrderMap);
|
||||
customerMap.put("10A", customerOne);
|
||||
customerMap.put("20B", customerTwo);
|
||||
customerMap.put("30C", customerThree);
|
||||
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue