Skip to content

Commit 398b0e3

Browse files
Merge pull request eugenp#402 from roshanathomas/master
Spring HATEOAS support
2 parents 0ac51ec + c2d84ae commit 398b0e3

9 files changed

Lines changed: 310 additions & 1 deletion

File tree

‎spring-security-rest/pom.xml‎

Lines changed: 9 additions & 1 deletion
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

@@ -329,4 +337,4 @@
329337

330338
</properties>
331339

332-
</project>
340+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.baeldung.persistence.model;
2+
3+
import java.util.Map;
4+
5+
import org.springframework.hateoas.ResourceSupport;
6+
7+
import com.fasterxml.jackson.annotation.JsonInclude;
8+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
9+
10+
@JsonInclude(Include.NON_NULL)
11+
public class Customer extends ResourceSupport {
12+
private String customerId;
13+
private String customerName;
14+
private String companyName;
15+
private Map<String, Order> orders;
16+
17+
public Customer() {
18+
super();
19+
}
20+
21+
public Customer(final String customerId, final String customerName, final String companyName) {
22+
super();
23+
this.customerId = customerId;
24+
this.customerName = customerName;
25+
this.companyName = companyName;
26+
}
27+
28+
public String getCustomerId() {
29+
return customerId;
30+
}
31+
32+
public void setCustomerId(final String customerId) {
33+
this.customerId = customerId;
34+
}
35+
36+
public String getCustomerName() {
37+
return customerName;
38+
}
39+
40+
public void setCustomerName(final String customerName) {
41+
this.customerName = customerName;
42+
}
43+
44+
public String getCompanyName() {
45+
return companyName;
46+
}
47+
48+
public void setCompanyName(final String companyName) {
49+
this.companyName = companyName;
50+
}
51+
52+
public Map<String, Order> getOrders() {
53+
return orders;
54+
}
55+
56+
public void setOrders(final Map<String, Order> orders) {
57+
this.orders = orders;
58+
}
59+
60+
}
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+
}

‎spring-security-rest/src/main/java/org/baeldung/spring/SecurityJavaConfig.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ protected void configure(final HttpSecurity http) throws Exception {// @formatte
4343
.and()
4444
.authorizeRequests()
4545
.antMatchers("/api/csrfAttacker*").permitAll()
46+
.antMatchers("/api/customers**").permitAll()
47+
.antMatchers("/api/customer/**").permitAll()
4648
.antMatchers("/api/**").authenticated()
4749
.and()
4850
.formLogin()
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.baeldung.web.service.OrderService;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.hateoas.Link;
11+
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RequestMethod;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
@RestController
18+
public class CustomerController {
19+
@Autowired
20+
private CustomerService customerService;
21+
22+
@Autowired
23+
private OrderService orderService;
24+
25+
@RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET)
26+
public Customer getCustomerById(@PathVariable final String customerId) {
27+
return customerService.getCustomerDetail(customerId);
28+
}
29+
30+
@RequestMapping(value = "/customer/{customerId}/{orderId}", method = RequestMethod.GET)
31+
public Order getOrderById(@PathVariable final String customerId, @PathVariable final String orderId) {
32+
return orderService.getOrderByIdForCustomer(customerId, orderId);
33+
}
34+
35+
@RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET)
36+
public List<Order> getOrdersForCustomer(@PathVariable final String customerId) {
37+
final List<Order> orders = orderService.getAllOrdersForCustomer(customerId);
38+
for (final Order order : orders) {
39+
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel();
40+
order.add(selfLink);
41+
}
42+
return orders;
43+
}
44+
45+
@RequestMapping(value = "/customers", method = RequestMethod.GET)
46+
public List<Customer> getAllCustomers() {
47+
final List<Customer> allCustomers = customerService.allCustomers();
48+
for (final Customer customer : allCustomers) {
49+
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel();
50+
customer.add(selfLink);
51+
if (orderService.getAllOrdersForCustomer(customer.getCustomerId()).size() > 0) {
52+
final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders");
53+
customer.add(ordersLink);
54+
}
55+
56+
}
57+
return allCustomers;
58+
}
59+
60+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung.web.service;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.persistence.model.Customer;
6+
7+
public interface CustomerService {
8+
9+
List<Customer> allCustomers();
10+
11+
Customer getCustomerDetail(final String id);
12+
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.springframework.stereotype.Service;
9+
10+
@Service
11+
public class CustomerServiceImpl implements CustomerService {
12+
13+
private HashMap<String, Customer> customerMap;
14+
15+
public CustomerServiceImpl() {
16+
17+
customerMap = new HashMap<>();
18+
19+
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
20+
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
21+
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
22+
23+
customerMap.put("10A", customerOne);
24+
customerMap.put("20B", customerTwo);
25+
customerMap.put("30C", customerThree);
26+
27+
}
28+
29+
@Override
30+
public List<Customer> allCustomers() {
31+
return new ArrayList<>(customerMap.values());
32+
}
33+
34+
@Override
35+
public Customer getCustomerDetail(final String customerId) {
36+
return customerMap.get(customerId);
37+
}
38+
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung.web.service;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.persistence.model.Order;
6+
7+
public interface OrderService {
8+
9+
List<Order> getAllOrdersForCustomer(String customerId);
10+
11+
Order getOrderByIdForCustomer(String customerId, String orderId);
12+
13+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 OrderServiceImpl implements OrderService {
13+
14+
private HashMap<String, Customer> customerMap;
15+
private HashMap<String, Order> customerOneOrderMap;
16+
private HashMap<String, Order> customerTwoOrderMap;
17+
private HashMap<String, Order> customerThreeOrderMap;
18+
19+
public OrderServiceImpl() {
20+
21+
customerMap = new HashMap<>();
22+
customerOneOrderMap = new HashMap<>();
23+
customerTwoOrderMap = new HashMap<>();
24+
customerThreeOrderMap = new HashMap<>();
25+
26+
customerOneOrderMap.put("001A", new Order("001A", 150.00, 25));
27+
customerOneOrderMap.put("002A", new Order("002A", 250.00, 15));
28+
29+
customerTwoOrderMap.put("002B", new Order("002B", 550.00, 325));
30+
customerTwoOrderMap.put("002B", new Order("002B", 450.00, 525));
31+
32+
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
33+
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
34+
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
35+
36+
customerOne.setOrders(customerOneOrderMap);
37+
customerTwo.setOrders(customerTwoOrderMap);
38+
customerThree.setOrders(customerThreeOrderMap);
39+
customerMap.put("10A", customerOne);
40+
customerMap.put("20B", customerTwo);
41+
customerMap.put("30C", customerThree);
42+
43+
}
44+
45+
@Override
46+
public List<Order> getAllOrdersForCustomer(final String customerId) {
47+
return new ArrayList<>(customerMap.get(customerId).getOrders().values());
48+
}
49+
50+
@Override
51+
public Order getOrderByIdForCustomer(final String customerId, final String orderId) {
52+
53+
final List<Order> orders = (List<Order>) customerMap.get(customerId).getOrders().values();
54+
Order selectedOrder = null;
55+
for (final Order order : orders) {
56+
if (order.getId().equals(orderId)) {
57+
selectedOrder = order;
58+
}
59+
}
60+
return selectedOrder;
61+
62+
}
63+
64+
}

0 commit comments

Comments
 (0)