Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Created June 19, 2015 14:45
Show Gist options
  • Save thomasdarimont/9a252f4d8d6914d8d559 to your computer and use it in GitHub Desktop.
Save thomasdarimont/9a252f4d8d6914d8d559 to your computer and use it in GitHub Desktop.
Spring Boot Redis custom json serializer example.
package demo;
import java.io.Serializable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
@SpringBootApplication
public class App {
@Bean
@Primary
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory rcf) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(rcf);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new JsonRedisSerializer());
return template;
}
static class JsonRedisSerializer implements RedisSerializer<Object> {
private final ObjectMapper om;
public JsonRedisSerializer() {
this.om = new ObjectMapper().enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);
}
@Override
public byte[] serialize(Object t) throws SerializationException {
try {
return om.writeValueAsBytes(t);
} catch (JsonProcessingException e) {
throw new SerializationException(e.getMessage(), e);
}
}
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
if(bytes == null){
return null;
}
try {
return om.readValue(bytes, Object.class);
} catch (Exception e) {
throw new SerializationException(e.getMessage(), e);
}
}
}
public static void main(String[] args) {
RedisOperations<String, Object> redis = SpringApplication.run(App.class, args).getBean(RedisOperations.class);
A a = new B();
System.out.println(a);
redis.opsForValue().set("foo", a);
A aa = (A)redis.opsForValue().get("foo");
System.out.println(aa);
}
public static class A implements Serializable{
long longProperty = System.currentTimeMillis();
String stringProperty = "foo";
public String getStringProperty() {
return stringProperty;
}
public void setStringProperty(String stringProperty) {
this.stringProperty = stringProperty;
}
@Override
public String toString() {
return "A [stringProperty=" + stringProperty + "]";
}
}
public static class B extends A {
int intProperty = 42;
public int getIntProperty() {
return intProperty;
}
public void setIntProperty(int intProperty) {
this.intProperty = intProperty;
}
@Override
public String toString() {
return "B [intProperty=" + intProperty + ", stringProperty=" + stringProperty + "]";
}
}
}
@longdq01
Copy link

thanks for sharing !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment