How to insert JSON using quarkus-redis-client?
I tried writing the json as a String, but I don’t know if it’s correct.
@Singleton
public class EmployeeService {
@Inject
RedisClient redisClient;
public void insert(Employee employee) throws JsonProcessingException{
ObjectMapper mapper = new ObjectMapper();
String str = mapper.writeValueAsString(employee);
redisClient.set(Arrays.asList("employee", str ));
}
Employee get(String key) throws JsonMappingException, JsonProcessingException {
///Response res = redisClient.get(key);
String str = redisClient.get(key).toString();
ObjectMapper mapper = new ObjectMapper();
Employee emp = mapper.readValue(str, Employee.class);
return emp;
}
}
source code: https://github.com/alissonmelonascimento/quarkus-app-redis
2
Answers
Redis does not support JSON out of the box. It does only support the following datatypes. So it is OK to store JSON as a string.
But if you really need JSON support, you can try installing RedisJSON module in your server.
Do you can use this line to persist the information at Redis, the unique thing that you need to change is to replace the " to ‘ in your JSON content.