How to store Key and Values as JSON in Redis using Spring Data Redis?










0















I am developing Spring Data Redis example with Spring Boot. In this example, I am looking to store all keys and values as json format. Using CrudRepository pattern to perform CRUD operations. Do I need to set any configurations to that keys can be stored as JSON ? I went through the link : https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/, but not clear where to set confogurations.



127.0.0.1:6379> SMEMBERS user:2be0135f-dbda-490c-8cf9-cd9a7bfdce1b:idx
1) "user:firstName:John"
2) "user:lastName:Kerr"
3) "user:role.roleName:API"
4) "user:middleName:Lima"
127.0.0.1:6379> HGETALL user:2be0135f-dbda-490c-8cf9-cd9a7bfdce1b
1) "_class"
2) "com.baeldung.spring.data.redis.model.User"
3) "id"
4) "2be0135f-dbda-490c-8cf9-cd9a7bfdce1b"
5) "firstName"
6) "John"
7) "middleName"
8) "Lima"
9) "lastName"
10) "Kerr"
11) "role.id"
12) "R2"
13) "role.roleName"
14) "API"
127.0.0.1:6379>


Role.java



@RedisHash("Role")
public class Role
private @Id String id;
private @Indexed String roleName;



User.java



@RedisHash("user")
public class User
private @Id String id;
private @Indexed String firstName;
private @Indexed String middleName;
private @Indexed String lastName;
private Role role;



RedisConfig.java



@Configuration
@ConfigurationProperties
@EnableRedisRepositories("com.baeldung.spring.data.redis.repository")
public class RedisConfig
@Bean
JedisConnectionFactory jedisConnectionFactory()
return new JedisConnectionFactory();

@Bean
RedisTemplate<Object, Object> redisTemplate()
final RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class));
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;




MainApp.java



@SpringBootApplication
@Transactional
public class MainAppDemo implements CommandLineRunner
RedisMappingContext mappingContext = new RedisMappingContext();
ExampleQueryMapper mapper = new ExampleQueryMapper(mappingContext, new PathIndexResolver(mappingContext));

@Autowired private UserRepository userRepository;

public static void main(String args)
SpringApplication.run(MainAppDemo.class, args);


@Override
public void run(String... args) throws Exception
Role role1 = Role.builder().id("R1").roleName("ADMIN").build();
User user1 = User.builder().firstName("Matt").middleName("Mike").lastName("Wixson").role(role1).build();

Role role2 = Role.builder().id("R2").roleName("API").build();
User user2 = User.builder().firstName("John").middleName("Lima").lastName("Kerr").role(role2).build();

userRepository.save(user1);
userRepository.save(user2);












share|improve this question
























  • Which of the options from chapters 5.7, 5.8 and 8.3 did you try and what problems did you have?

    – Tomasz Poradowski
    Nov 16 '18 at 8:26











  • I've not used RedisTemplate for any of the CRUD operation. Its via the CRUD Repository. Please suggest now.

    – PAA
    Nov 16 '18 at 12:15












  • @Tomasz Poradowski - Could you please guide ?

    – PAA
    Nov 23 '18 at 14:24















0















I am developing Spring Data Redis example with Spring Boot. In this example, I am looking to store all keys and values as json format. Using CrudRepository pattern to perform CRUD operations. Do I need to set any configurations to that keys can be stored as JSON ? I went through the link : https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/, but not clear where to set confogurations.



127.0.0.1:6379> SMEMBERS user:2be0135f-dbda-490c-8cf9-cd9a7bfdce1b:idx
1) "user:firstName:John"
2) "user:lastName:Kerr"
3) "user:role.roleName:API"
4) "user:middleName:Lima"
127.0.0.1:6379> HGETALL user:2be0135f-dbda-490c-8cf9-cd9a7bfdce1b
1) "_class"
2) "com.baeldung.spring.data.redis.model.User"
3) "id"
4) "2be0135f-dbda-490c-8cf9-cd9a7bfdce1b"
5) "firstName"
6) "John"
7) "middleName"
8) "Lima"
9) "lastName"
10) "Kerr"
11) "role.id"
12) "R2"
13) "role.roleName"
14) "API"
127.0.0.1:6379>


Role.java



@RedisHash("Role")
public class Role
private @Id String id;
private @Indexed String roleName;



User.java



@RedisHash("user")
public class User
private @Id String id;
private @Indexed String firstName;
private @Indexed String middleName;
private @Indexed String lastName;
private Role role;



RedisConfig.java



@Configuration
@ConfigurationProperties
@EnableRedisRepositories("com.baeldung.spring.data.redis.repository")
public class RedisConfig
@Bean
JedisConnectionFactory jedisConnectionFactory()
return new JedisConnectionFactory();

@Bean
RedisTemplate<Object, Object> redisTemplate()
final RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class));
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;




MainApp.java



@SpringBootApplication
@Transactional
public class MainAppDemo implements CommandLineRunner
RedisMappingContext mappingContext = new RedisMappingContext();
ExampleQueryMapper mapper = new ExampleQueryMapper(mappingContext, new PathIndexResolver(mappingContext));

@Autowired private UserRepository userRepository;

public static void main(String args)
SpringApplication.run(MainAppDemo.class, args);


@Override
public void run(String... args) throws Exception
Role role1 = Role.builder().id("R1").roleName("ADMIN").build();
User user1 = User.builder().firstName("Matt").middleName("Mike").lastName("Wixson").role(role1).build();

Role role2 = Role.builder().id("R2").roleName("API").build();
User user2 = User.builder().firstName("John").middleName("Lima").lastName("Kerr").role(role2).build();

userRepository.save(user1);
userRepository.save(user2);












share|improve this question
























  • Which of the options from chapters 5.7, 5.8 and 8.3 did you try and what problems did you have?

    – Tomasz Poradowski
    Nov 16 '18 at 8:26











  • I've not used RedisTemplate for any of the CRUD operation. Its via the CRUD Repository. Please suggest now.

    – PAA
    Nov 16 '18 at 12:15












  • @Tomasz Poradowski - Could you please guide ?

    – PAA
    Nov 23 '18 at 14:24













0












0








0


1






I am developing Spring Data Redis example with Spring Boot. In this example, I am looking to store all keys and values as json format. Using CrudRepository pattern to perform CRUD operations. Do I need to set any configurations to that keys can be stored as JSON ? I went through the link : https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/, but not clear where to set confogurations.



127.0.0.1:6379> SMEMBERS user:2be0135f-dbda-490c-8cf9-cd9a7bfdce1b:idx
1) "user:firstName:John"
2) "user:lastName:Kerr"
3) "user:role.roleName:API"
4) "user:middleName:Lima"
127.0.0.1:6379> HGETALL user:2be0135f-dbda-490c-8cf9-cd9a7bfdce1b
1) "_class"
2) "com.baeldung.spring.data.redis.model.User"
3) "id"
4) "2be0135f-dbda-490c-8cf9-cd9a7bfdce1b"
5) "firstName"
6) "John"
7) "middleName"
8) "Lima"
9) "lastName"
10) "Kerr"
11) "role.id"
12) "R2"
13) "role.roleName"
14) "API"
127.0.0.1:6379>


Role.java



@RedisHash("Role")
public class Role
private @Id String id;
private @Indexed String roleName;



User.java



@RedisHash("user")
public class User
private @Id String id;
private @Indexed String firstName;
private @Indexed String middleName;
private @Indexed String lastName;
private Role role;



RedisConfig.java



@Configuration
@ConfigurationProperties
@EnableRedisRepositories("com.baeldung.spring.data.redis.repository")
public class RedisConfig
@Bean
JedisConnectionFactory jedisConnectionFactory()
return new JedisConnectionFactory();

@Bean
RedisTemplate<Object, Object> redisTemplate()
final RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class));
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;




MainApp.java



@SpringBootApplication
@Transactional
public class MainAppDemo implements CommandLineRunner
RedisMappingContext mappingContext = new RedisMappingContext();
ExampleQueryMapper mapper = new ExampleQueryMapper(mappingContext, new PathIndexResolver(mappingContext));

@Autowired private UserRepository userRepository;

public static void main(String args)
SpringApplication.run(MainAppDemo.class, args);


@Override
public void run(String... args) throws Exception
Role role1 = Role.builder().id("R1").roleName("ADMIN").build();
User user1 = User.builder().firstName("Matt").middleName("Mike").lastName("Wixson").role(role1).build();

Role role2 = Role.builder().id("R2").roleName("API").build();
User user2 = User.builder().firstName("John").middleName("Lima").lastName("Kerr").role(role2).build();

userRepository.save(user1);
userRepository.save(user2);












share|improve this question
















I am developing Spring Data Redis example with Spring Boot. In this example, I am looking to store all keys and values as json format. Using CrudRepository pattern to perform CRUD operations. Do I need to set any configurations to that keys can be stored as JSON ? I went through the link : https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/, but not clear where to set confogurations.



127.0.0.1:6379> SMEMBERS user:2be0135f-dbda-490c-8cf9-cd9a7bfdce1b:idx
1) "user:firstName:John"
2) "user:lastName:Kerr"
3) "user:role.roleName:API"
4) "user:middleName:Lima"
127.0.0.1:6379> HGETALL user:2be0135f-dbda-490c-8cf9-cd9a7bfdce1b
1) "_class"
2) "com.baeldung.spring.data.redis.model.User"
3) "id"
4) "2be0135f-dbda-490c-8cf9-cd9a7bfdce1b"
5) "firstName"
6) "John"
7) "middleName"
8) "Lima"
9) "lastName"
10) "Kerr"
11) "role.id"
12) "R2"
13) "role.roleName"
14) "API"
127.0.0.1:6379>


Role.java



@RedisHash("Role")
public class Role
private @Id String id;
private @Indexed String roleName;



User.java



@RedisHash("user")
public class User
private @Id String id;
private @Indexed String firstName;
private @Indexed String middleName;
private @Indexed String lastName;
private Role role;



RedisConfig.java



@Configuration
@ConfigurationProperties
@EnableRedisRepositories("com.baeldung.spring.data.redis.repository")
public class RedisConfig
@Bean
JedisConnectionFactory jedisConnectionFactory()
return new JedisConnectionFactory();

@Bean
RedisTemplate<Object, Object> redisTemplate()
final RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class));
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;




MainApp.java



@SpringBootApplication
@Transactional
public class MainAppDemo implements CommandLineRunner
RedisMappingContext mappingContext = new RedisMappingContext();
ExampleQueryMapper mapper = new ExampleQueryMapper(mappingContext, new PathIndexResolver(mappingContext));

@Autowired private UserRepository userRepository;

public static void main(String args)
SpringApplication.run(MainAppDemo.class, args);


@Override
public void run(String... args) throws Exception
Role role1 = Role.builder().id("R1").roleName("ADMIN").build();
User user1 = User.builder().firstName("Matt").middleName("Mike").lastName("Wixson").role(role1).build();

Role role2 = Role.builder().id("R2").roleName("API").build();
User user2 = User.builder().firstName("John").middleName("Lima").lastName("Kerr").role(role2).build();

userRepository.save(user1);
userRepository.save(user2);









spring redis spring-data-redis






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:12







PAA

















asked Nov 13 '18 at 16:29









PAAPAA

2,55222036




2,55222036












  • Which of the options from chapters 5.7, 5.8 and 8.3 did you try and what problems did you have?

    – Tomasz Poradowski
    Nov 16 '18 at 8:26











  • I've not used RedisTemplate for any of the CRUD operation. Its via the CRUD Repository. Please suggest now.

    – PAA
    Nov 16 '18 at 12:15












  • @Tomasz Poradowski - Could you please guide ?

    – PAA
    Nov 23 '18 at 14:24

















  • Which of the options from chapters 5.7, 5.8 and 8.3 did you try and what problems did you have?

    – Tomasz Poradowski
    Nov 16 '18 at 8:26











  • I've not used RedisTemplate for any of the CRUD operation. Its via the CRUD Repository. Please suggest now.

    – PAA
    Nov 16 '18 at 12:15












  • @Tomasz Poradowski - Could you please guide ?

    – PAA
    Nov 23 '18 at 14:24
















Which of the options from chapters 5.7, 5.8 and 8.3 did you try and what problems did you have?

– Tomasz Poradowski
Nov 16 '18 at 8:26





Which of the options from chapters 5.7, 5.8 and 8.3 did you try and what problems did you have?

– Tomasz Poradowski
Nov 16 '18 at 8:26













I've not used RedisTemplate for any of the CRUD operation. Its via the CRUD Repository. Please suggest now.

– PAA
Nov 16 '18 at 12:15






I've not used RedisTemplate for any of the CRUD operation. Its via the CRUD Repository. Please suggest now.

– PAA
Nov 16 '18 at 12:15














@Tomasz Poradowski - Could you please guide ?

– PAA
Nov 23 '18 at 14:24





@Tomasz Poradowski - Could you please guide ?

– PAA
Nov 23 '18 at 14:24












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285443%2fhow-to-store-key-and-values-as-json-in-redis-using-spring-data-redis%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285443%2fhow-to-store-key-and-values-as-json-in-redis-using-spring-data-redis%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto