Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @EnableRedisRepositories
- public interface CustomerRepository extends JpaRepository<Customer, String> {
- Optional<Customer> findByFirstName(String firstName);
- }
- @Service
- @Transactional
- public class CustomerService {
- private final CustomerRepository repository;
- public CustomerService(CustomerRepository repository) {
- this.repository = repository;
- }
- ...
- public Optional<Customer> retrieveCustomersByName(String name) {
- return repository.findByFirstName(name);
- }
- }
- @RestController
- @RequestMapping("/customers")
- public class CustomerController {
- @Autowired
- private CustomerService customerService;
- ....
- @GetMapping("/{firstName}")
- public ResponseEntity<?> getCustomers(@PathVariable String firstName) {
- Optional<?> customer = customerService.retrieveCustomersByName(firstName);
- if (!customer.isPresent()) {
- return ResponseEntity.badRequest().build();
- }
- return ResponseEntity.ok(customer);
- }
- }
Add Comment
Please, Sign In to add comment