Spring HATEOAS support - multiple links

This commit is contained in:
Roshan Thomas 2016-04-06 23:45:35 -04:00
parent 4ec8015949
commit cdeb7147b4
3 changed files with 11 additions and 3 deletions

View File

@ -43,6 +43,8 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
.and() .and()
.authorizeRequests() .authorizeRequests()
.antMatchers("/api/csrfAttacker*").permitAll() .antMatchers("/api/csrfAttacker*").permitAll()
.antMatchers("/api/customers**").permitAll()
.antMatchers("/api/customer/**").permitAll()
.antMatchers("/api/**").authenticated() .antMatchers("/api/**").authenticated()
.and() .and()
.formLogin() .formLogin()

View File

@ -39,9 +39,10 @@ public class CustomerController {
for (final Customer customer : allCustomers) { for (final Customer customer : allCustomers) {
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel(); final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel();
customer.add(selfLink); customer.add(selfLink);
final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders"); if (customer.getOrders().values().size() > 0) {
customer.add(ordersLink); final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders");
customer.add(ordersLink);
}
for (final Order order : customer.getOrders().values()) { for (final Order order : customer.getOrders().values()) {
final Link orderLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderByIdForCustomer(customer.getCustomerId(), order.getOrderId())).withRel("order"); final Link orderLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderByIdForCustomer(customer.getCustomerId(), order.getOrderId())).withRel("order");
order.add(orderLink); order.add(orderLink);

View File

@ -14,15 +14,18 @@ public class CustomerServiceImpl implements CustomerService {
private HashMap<String, Customer> customerMap; private HashMap<String, Customer> customerMap;
private HashMap<String, Order> customerOneOrderMap; private HashMap<String, Order> customerOneOrderMap;
private HashMap<String, Order> customerTwoOrderMap; private HashMap<String, Order> customerTwoOrderMap;
private HashMap<String, Order> customerThreeOrderMap;
public CustomerServiceImpl() { public CustomerServiceImpl() {
customerMap = new HashMap<>(); customerMap = new HashMap<>();
customerOneOrderMap = new HashMap<>(); customerOneOrderMap = new HashMap<>();
customerTwoOrderMap = new HashMap<>(); customerTwoOrderMap = new HashMap<>();
customerThreeOrderMap = new HashMap<>();
final Customer customerOne = new Customer("10A", "Jane", "ABC Company"); final Customer customerOne = new Customer("10A", "Jane", "ABC Company");
final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company"); final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company");
final Customer customerThree = new Customer("30C", "Tim", "CKV Company");
customerOneOrderMap.put("001A", new Order("001A", 150.00, 25)); customerOneOrderMap.put("001A", new Order("001A", 150.00, 25));
customerOneOrderMap.put("002A", new Order("002A", 250.00, 15)); customerOneOrderMap.put("002A", new Order("002A", 250.00, 15));
@ -32,8 +35,10 @@ public class CustomerServiceImpl implements CustomerService {
customerOne.setOrders(customerOneOrderMap); customerOne.setOrders(customerOneOrderMap);
customerTwo.setOrders(customerTwoOrderMap); customerTwo.setOrders(customerTwoOrderMap);
customerThree.setOrders(customerThreeOrderMap);
customerMap.put("10A", customerOne); customerMap.put("10A", customerOne);
customerMap.put("20B", customerTwo); customerMap.put("20B", customerTwo);
customerMap.put("30C", customerThree);
} }