Guest User

Untitled

a guest
Jan 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. @EnableRedisRepositories
  2. public interface CustomerRepository extends JpaRepository<Customer, String> {
  3.  
  4. Optional<Customer> findByFirstName(String firstName);
  5. }
  6.  
  7. @Service
  8. @Transactional
  9. public class CustomerService {
  10. private final CustomerRepository repository;
  11.  
  12. public CustomerService(CustomerRepository repository) {
  13. this.repository = repository;
  14. }
  15.  
  16. ...
  17.  
  18.  
  19. public Optional<Customer> retrieveCustomersByName(String name) {
  20. return repository.findByFirstName(name);
  21. }
  22. }
  23.  
  24. @RestController
  25. @RequestMapping("/customers")
  26. public class CustomerController {
  27.  
  28. @Autowired
  29. private CustomerService customerService;
  30.  
  31. ....
  32.  
  33. @GetMapping("/{firstName}")
  34. public ResponseEntity<?> getCustomers(@PathVariable String firstName) {
  35. Optional<?> customer = customerService.retrieveCustomersByName(firstName);
  36. if (!customer.isPresent()) {
  37. return ResponseEntity.badRequest().build();
  38. }
  39. return ResponseEntity.ok(customer);
  40. }
  41.  
  42.  
  43. }
Add Comment
Please, Sign In to add comment