How to make a query by passing parameters with spring boot and redis using Jparepository?

Asked

Viewed 923 times

1

I have the model:

@Data
@RedisHash("customer")
public class Customer {
    @Id
    private String id;
    @NotNull
    private String firstName;
   ... 

I have the following interface:

@EnableRedisRepositories
public interface CustomerRepository extends JpaRepository<Customer, String> {

   Optional<Customer>  findByFirstName(String firstName);
}

The Service:

@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);
        }
}

And the following restcontroller:

@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);
    }


}

All methods of crud are ok!

However when I do the query by Postman it does not return any redis result. What should I do?

url used in Postman:

http://localhost:9000/api/customers/paulo

application.yml file:

server:
  port: 9000
  servlet:
    contextPath: /api
  • Sorry to be unaware, but I have not seen "api" mapped anywhere in your code, it is necessary that it is in the url?

  • @Scarabelo That nothing, the "api" is mapped in the application.properties file, but in my case, this mapped in the application.yml file , that gives in the same. I’ll put it in the code above -

1 answer

1


In the template class add the annotation @Indexed package org.springframework.data.redis.core.index.Indexed;

@Data
@RedisHash("customer")
public class Customer {
    @Id
    private String id;
    @NotNull
    @Indexed
    private String firstName;
   ... 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.