question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to change the default serialization of the framework to my own JSON serialization

See original GitHub issue
 @Bean("reactiveRedisTemplate")
    public ReactiveRedisTemplate<String, Object> ReactiveRedisTemplate() {
        GenericFastJsonRedisSerializer jsonRedisSerializer = new GenericFastJsonRedisSerializer();
        RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext
                .newSerializationContext().key(jsonRedisSerializer).value(jsonRedisSerializer)
                .hashKey(jsonRedisSerializer).hashValue(jsonRedisSerializer).build();
        return new ReactiveRedisTemplate(lettuceConnectionFactory(), serializationContext);
    }

but I find

org.springframework.session.data.redis.config.annotation.web.server.RedisWebSessionConfiguration 
have a private method:
private ReactiveRedisTemplate<String, Object> createReactiveRedisTemplate() {
		RedisSerializer<String> keySerializer = new StringRedisSerializer();
		RedisSerializer<Object> valueSerializer = new JdkSerializationRedisSerializer(
				this.classLoader);
		RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
				.<String, Object>newSerializationContext(valueSerializer)
				.key(keySerializer).hashKey(keySerializer).build();
		return new ReactiveRedisTemplate<>(this.redisConnectionFactory,
				serializationContext);
	}


My configuration does not work at all.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
vpaviccommented, Oct 30, 2018

OK, so as suspected, you’re using Spring Session 2.0.x (i.e. Apple) where we don’t have a first-class support for supplying a custom serialization mechanism.

You have two options; either update to Spring Session 2.1.x (i.e. Bean) - note that we’ve just release Bean-RELEASE yesterday; if you cannot upgrade, you need to manually register ReactiveRedisOperationsSessionRepository and configure it with desired ReactiveRedisTemplate. For example:

@EnableRedisWebSession
public class SessionConfiguration {

    @Bean
    public ReactiveRedisOperationsSessionRepository sessionRepository() {
        // either configure reactiveRedisTemplate for Spring Session here or inject it
        // ...
        ReactiveRedisOperationsSessionRepository sessionRepository =
            new ReactiveRedisOperationsSessionRepository(reactiveRedisTemplate);
    }

}

When you upgrade the Spring Session 2.1.x (i.e. Bean) you will simply replace the custom ReactiveRedisOperationsSessionRepository bean declaration with the RedisSerializer<Object> named springSessionDefaultRedisSerializer that will be automatically picked up:

@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
    // return you Redis serializer implementation
}

Does this help?

0reactions
vpaviccommented, Oct 31, 2018

Thanks for the follow up and kind words @masterchengsheng. Closing this as answered.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write custom converters for JSON serialization - .NET
Learn how to create custom converters for the JSON serialization classes that ... To override the default behavior of a built-in converter.
Read more >
Setting the default JSON serializer in ASP.NET MVC
In ASP.Net MVC4 the default JavaScript serializer which is used in the JsonResult class is still the JavaScriptSerializer (you can check it in...
Read more >
Customize your Java-JSON serialization using Jackson ...
As you can see, Jackson serializes our Java object to JSON including all the “accessible fields” and uses an epoch to represent our...
Read more >
Setting JSON Serialization Configuration At Runtime On A ...
Within JSON.net, there isn't any ability for this to do it out of the box. In my case I'm looking to use a...
Read more >
JSON Serialization in Rails: A Complete Guide - ButterCMS
The JSON serialization process consists of two stages: data preparation and transformation to the JSON format. Data preparation. Data ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found