Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Inconsistent between the way RedisModulesOperations and the Redis OM repository are configured or handle JSON serialization #523

Open
motazco135 opened this issue Oct 31, 2024 · 0 comments

Comments

@motazco135
Copy link

When i use Redis OM Repository to findById it can not mapp some fields in the document class ,
however when use RedisModulesOperations with object mapper it works fine

Redis JSON Document :

{
"id": "1", 
"first_name": "test", 
"last_name": "test", 
"email": "a@test.com",
 "mobile": "0000000000",
 "created_at": 1730325172776,
 "updated_at": 1730340769219
}

Document Class :

import com.fasterxml.jackson.annotation.JsonProperty;
import com.redis.om.spring.annotations.Document;
import lombok.*;

@Data
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Document("customer_profile_t_customers")
public class Customer {
    private String id;
    @JsonProperty("first_name")
    private String firstName;
    @JsonProperty("last_name")
    private String lastName;
    private String email;
    private String mobile;
}

Repository Method:

public Optional<Customer> getCustomerProfile(String customerId)  {
        Optional<Customer> optionalCustomer = customerRepository.findById(customerId);
        if(optionalCustomer.isEmpty()){
            return Optional.empty();
        }
        return optionalCustomer;
    }

OutPut :

{
  "id": "1",
  "email": "a@test.com",
  "mobile": "0000000000",
  "first_name": null,
  "last_name": null
}

RedisModulesOperations Method:

public Optional<Customer> getCustomer(String customerId) {
        try {
            String key = "customer_profile_t_customers:" + customerId;
            log.info("key : {}", key);
            String customerProfileStr = redisModulesOperations.opsForJSON().get(key);
            return customerProfileStr != null ? Optional.of(objectMapper.readValue(customerProfileStr, Customer.class)) : Optional.empty();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }

OutPut :

{
  "id": "1",
  "email": "a@test.com",
  "mobile": "0000000000",
  "first_name": "test",
  "last_name": "test"
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant