Skip to content

Commit 4ec8015

Browse files
committed
Spring HATEOAS support
1 parent 08c8d82 commit 4ec8015

6 files changed

Lines changed: 254 additions & 0 deletions

File tree

‎spring-security-rest/pom.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@
7676
<artifactId>spring-webmvc</artifactId>
7777
<version>${org.springframework.version}</version>
7878
</dependency>
79+
80+
<!-- Spring HATEOAS -->
81+
82+
<dependency>
83+
<groupId>org.springframework.hateoas</groupId>
84+
<artifactId>spring-hateoas</artifactId>
85+
<version>0.19.0.RELEASE</version>
86+
</dependency>
7987

8088
<!-- web -->
8189

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.baeldung.persistence.model;
2+
3+
import java.util.Map;
4+
5+
import org.springframework.hateoas.ResourceSupport;
6+
7+
public class Customer extends ResourceSupport {
8+
private String customerId;
9+
private String customerName;
10+
private String companyName;
11+
private Map<String, Order> orders;
12+
13+
public Customer() {
14+
super();
15+
}
16+
17+
public Customer(final String customerId, final String customerName, final String companyName) {
18+
super();
19+
this.customerId = customerId;
20+
this.customerName = customerName;
21+
this.companyName = companyName;
22+
}
23+
24+
public String getCustomerId() {
25+
return customerId;
26+
}
27+
28+
public void setCustomerId(final String customerId) {
29+
this.customerId = customerId;
30+
}
31+
32+
public String getCustomerName() {
33+
return customerName;
34+
}
35+
36+
public void setCustomerName(final String customerName) {
37+
this.customerName = customerName;
38+
}
39+
40+
public String getCompanyName() {
41+
return companyName;
42+
}
43+
44+
public void setCompanyName(final String companyName) {
45+
this.companyName = companyName;
46+
}
47+
48+
public Map<String, Order> getOrders() {
49+
return orders;
50+
}
51+
52+
public void setOrders(final Map<String, Order> orders) {
53+
this.orders = orders;
54+
}
55+
56+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.baeldung.persistence.model;
2+
3+
import org.springframework.hateoas.ResourceSupport;
4+
5+
public class Order extends ResourceSupport {
6+
private String orderId;
7+
private double price;
8+
private int quantity;
9+
10+
public Order() {
11+
super();
12+
}
13+
14+
public Order(final String orderId, final double price, final int quantity) {
15+
super();
16+
this.orderId = orderId;
17+
this.price = price;
18+
this.quantity = quantity;
19+
}
20+
21+
public String getOrderId() {
22+
return orderId;
23+
}
24+
25+
public void setOrderId(final String orderId) {
26+
this.orderId = orderId;
27+
}
28+
29+
public double getPrice() {
30+
return price;
31+
}
32+
33+
public void setPrice(final double price) {
34+
this.price = price;
35+
}
36+
37+
public int getQuantity() {
38+
return quantity;
39+
}
40+
41+
public void setQuantity(final int quantity) {
42+
this.quantity = quantity;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "Order [orderId=" + orderId + ", price=" + price + ", quantity=" + quantity + "]";
48+
}
49+
50+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.baeldung.web.controller;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.persistence.model.Customer;
6+
import org.baeldung.persistence.model.Order;
7+
import org.baeldung.web.service.CustomerService;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.hateoas.Link;
10+
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
@RestController
17+
public class CustomerController {
18+
@Autowired
19+
private CustomerService customerService;
20+
21+
@RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET)
22+
public Customer getCustomerById(@PathVariable final String customerId) {
23+
return customerService.getCustomerDetail(customerId);
24+
}
25+
26+
@RequestMapping(value = "/customer/{customerId}/{orderId}", method = RequestMethod.GET)
27+
public Order getOrderByIdForCustomer(@PathVariable final String customerId, @PathVariable final String orderId) {
28+
return customerService.getOrderByIdForCustomer(customerId, orderId);
29+
}
30+
31+
@RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET)
32+
public List<Order> getOrdersForCustomer(@PathVariable final String customerId) {
33+
return customerService.getAllOrdersForCustomer(customerId);
34+
}
35+
36+
@RequestMapping(value = "/customers", method = RequestMethod.GET)
37+
public List<Customer> getAllCustomers() {
38+
final List<Customer> allCustomers = customerService.allCustomers();
39+
for (final Customer customer : allCustomers) {
40+
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel();
41+
customer.add(selfLink);
42+
final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders");
43+
customer.add(ordersLink);
44+
45+
for (final Order order : customer.getOrders().values()) {
46+
final Link orderLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderByIdForCustomer(customer.getCustomerId(), order.getOrderId())).withRel("order");
47+
order.add(orderLink);
48+
}
49+
}
50+
return allCustomers;
51+
}
52+
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.web.service;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.persistence.model.Customer;
6+
import org.baeldung.persistence.model.Order;
7+
8+
public interface CustomerService {
9+
10+
List<Customer> allCustomers();
11+
12+
Customer getCustomerDetail(final String id);
13+
14+
List<Order> getAllOrdersForCustomer(String customerId);
15+
16+
Order getOrderByIdForCustomer(String customerId, String orderId);
17+
18+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.baeldung.web.service;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
7+
import org.baeldung.persistence.model.Customer;
8+
import org.baeldung.persistence.model.Order;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public class CustomerServiceImpl implements CustomerService {
13+
14+
private HashMap<String, Customer> customerMap;
15+
private HashMap<String, Order> customerOneOrderMap;
16+
private HashMap<String, Order> customerTwoOrderMap;
17+
18+
public CustomerServiceImpl() {
19+
20+
customerMap = new HashMap<>();
21+
customerOneOrderMap = new HashMap<>();
22+
customerTwoOrderMap = new HashMap<>();
23+
24+
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
25+
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
26+
27+
customerOneOrderMap.put("001A", new Order("001A", 150.00, 25));
28+
customerOneOrderMap.put("002A", new Order("002A", 250.00, 15));
29+
30+
customerTwoOrderMap.put("002B", new Order("002B", 550.00, 325));
31+
customerTwoOrderMap.put("002B", new Order("002B", 450.00, 525));
32+
33+
customerOne.setOrders(customerOneOrderMap);
34+
customerTwo.setOrders(customerTwoOrderMap);
35+
customerMap.put("10A", customerOne);
36+
customerMap.put("20B", customerTwo);
37+
38+
}
39+
40+
@Override
41+
public List<Customer> allCustomers() {
42+
return new ArrayList<>(customerMap.values());
43+
}
44+
45+
@Override
46+
public Customer getCustomerDetail(final String customerId) {
47+
return customerMap.get(customerId);
48+
}
49+
50+
@Override
51+
public List<Order> getAllOrdersForCustomer(final String customerId) {
52+
return new ArrayList<>(customerMap.get(customerId).getOrders().values());
53+
}
54+
55+
@Override
56+
public Order getOrderByIdForCustomer(final String customerId, final String orderId) {
57+
58+
final List<Order> orders = (List<Order>) customerMap.get(customerId).getOrders().values();
59+
Order selectedOrder = null;
60+
for (final Order order : orders) {
61+
if (order.getId().equals(orderId)) {
62+
selectedOrder = order;
63+
}
64+
}
65+
return selectedOrder;
66+
67+
}
68+
69+
}

0 commit comments

Comments
 (0)