Spring Data RedisTemplate抛出SerializationException
在Spring Boot中使用RedisTemplate时, 出现了这样的情况:
- 当使用
new ArrayList<String>(Arrays.asList("abc", "def"))
创建List时, 可以正常存入Redis; - 而使用
new ArrayList<String>(){{add("abc"); add("def");}}
创建List时, 则会抛出异常.
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis() {
ArrayList<String> strings = new ArrayList<String>(){{add("abc"); add("def");}};
//ArrayList<String> strings = new ArrayList<String>(Arrays.asList("abc", "def"));
try {
redisTemplate.opsForValue().set("list", strings);
}
catch (Exception e) {
e.printStackTrace();
}
}
异常如下:
org.springframework.data.redis.serializer.SerializationException: Cannot serialize;
nested exception is org.springframework.core.serializer.support.SerializationFailedException:
Failed to serialize object using DefaultSerializer;
nested exception is java.io.NotSerializableException: com.example.demo.DemoApplicationTests
Spring Boot版本:
Spring Boot(v2.3.7.RELEASE)
不知道出现这样情况的原因是什么?
希望能够得到大佬的指点! 不胜感激!