Skip to content

Commit cdeb714

Browse files
committed
Spring HATEOAS support - multiple links
1 parent 4ec8015 commit cdeb714

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

‎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()

‎spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public List<Customer> getAllCustomers() {
3939
for (final Customer customer : allCustomers) {
4040
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel();
4141
customer.add(selfLink);
42-
final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders");
43-
customer.add(ordersLink);
44-
42+
if (customer.getOrders().values().size() > 0) {
43+
final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders");
44+
customer.add(ordersLink);
45+
}
4546
for (final Order order : customer.getOrders().values()) {
4647
final Link orderLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderByIdForCustomer(customer.getCustomerId(), order.getOrderId())).withRel("order");
4748
order.add(orderLink);

‎spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ public class CustomerServiceImpl implements CustomerService {
1414
private HashMap<String, Customer> customerMap;
1515
private HashMap<String, Order> customerOneOrderMap;
1616
private HashMap<String, Order> customerTwoOrderMap;
17+
private HashMap<String, Order> customerThreeOrderMap;
1718

1819
public CustomerServiceImpl() {
1920

2021
customerMap = new HashMap<>();
2122
customerOneOrderMap = new HashMap<>();
2223
customerTwoOrderMap = new HashMap<>();
24+
customerThreeOrderMap = new HashMap<>();
2325

2426
final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
2527
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
28+
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
2629

2730
customerOneOrderMap.put("001A", new Order("001A", 150.00, 25));
2831
customerOneOrderMap.put("002A", new Order("002A", 250.00, 15));
@@ -32,8 +35,10 @@ public CustomerServiceImpl() {
3235

3336
customerOne.setOrders(customerOneOrderMap);
3437
customerTwo.setOrders(customerTwoOrderMap);
38+
customerThree.setOrders(customerThreeOrderMap);
3539
customerMap.put("10A", customerOne);
3640
customerMap.put("20B", customerTwo);
41+
customerMap.put("30C", customerThree);
3742

3843
}
3944

0 commit comments

Comments
 (0)