hateoas - minor changes for self and method link creation

This commit is contained in:
slavisa-baeldung
2016-04-21 12:28:28 +01:00
parent 398b0e30f6
commit 4f410a7e11
3 changed files with 39 additions and 36 deletions
@@ -43,7 +43,6 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
.and()
.authorizeRequests()
.antMatchers("/api/csrfAttacker*").permitAll()
.antMatchers("/api/customers**").permitAll()
.antMatchers("/api/customer/**").permitAll()
.antMatchers("/api/**").authenticated()
.and()
@@ -1,20 +1,23 @@
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;
import java.util.List;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
@RestController
@RequestMapping(value = "/customer")
public class CustomerController {
@Autowired
private CustomerService customerService;
@@ -22,34 +25,36 @@ public class CustomerController {
@Autowired
private OrderService orderService;
@RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET)
@RequestMapping(value = "/{customerId}", method = RequestMethod.GET)
public Customer getCustomerById(@PathVariable final String customerId) {
return customerService.getCustomerDetail(customerId);
}
@RequestMapping(value = "/customer/{customerId}/{orderId}", method = RequestMethod.GET)
@RequestMapping(value = "/{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)
@RequestMapping(value = "/{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();
final Link selfLink = linkTo(methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel();
order.add(selfLink);
}
return orders;
}
@RequestMapping(value = "/customers", method = RequestMethod.GET)
@RequestMapping(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();
String customerId = customer.getCustomerId();
Link selfLink = linkTo(CustomerController.class).slash(customerId).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");
if (orderService.getAllOrdersForCustomer(customerId).size() > 0) {
List<Order> methodLinkBuilder = methodOn(CustomerController.class).getOrdersForCustomer(customerId);
final Link ordersLink = linkTo(methodLinkBuilder).withRel("allOrders");
customer.add(ordersLink);
}