Redis deserialization error Could not read JSON: Can not construct instance of `java.util.ArrayList $ SubList`

Redis deserialization error

Redis caching recently used to do that with the good, a return to the existing method of error in getting from Redis cache, the cache can not obtain from the error message to see, it can not be made serialized. Here the record about the solution.

Direct paste error:
Could not read JSON: Cannot construct instance of java.util.ArrayList$SubList(no Creators, like default construct, exist): no default no-arguments constructor found

Because the sequence of the object is more complex, multiple nested List objects, due to see the error message prompts the words "no Creators, like default contract" and so on, that is the lack of a no-argument constructor, on the plus lombok @ NoArgsConstructor notes, or to re-test the same mistake, in fact @NoArgsConstructor not have to be added, as it has been added @AllArgsConstructor annotated.
So they close look at the error message, "SubList", the culprit is this method. Our code does cache method uses Sublis ArrayList object has been intercepted. Because of the use of this method, resulting cache object can not be deserialized.
Let's look at Sublist source:
subList
subList method returns a List, the List is an interface, inherited from the Collection, and Collection also interface, a look at the layers of the past, are not implement the Serializable interface, it can not be serialized / deserialization.

List
See here, we should understand that the object ArrayList.subList method returns a sublist type of view, this is a sublist type of internal ArrayList class, and does not support serialization. The meaning of the view that the number of elements in it has changed, but the operation in which the elements of the original list is still the operation is not new share list. If you change the original list, it will throw an exception ConcurrentModificationException.

The root cause is found, take a look at how to solve it.
The solution is quite simple, to re-create a List to achieve serialization, after the interception list is stored in order to achieve serializable.

// 原错误代码
bannerList = bannerList.subList(0, SLIDING_SIZE); // 修改后的正确代码 bannerList = new ArrayList<>(slidingBanner.subList(0, SLIDING_SIZE)); 

prompt

When using Redis cache objects for cached objects, due to the limitations JSON serialization Jackson tool must be used lombok of @Data and @AllArgsConstructor modified, whether the person can emerge into the cache Redis but can not deserialize problem. Remember not to cache object will be declared as final!
See, reference may https://github.com/FasterXML/jackson-docs/wiki/JacksonPolymorphicDeserialization


Welcome to the number of public attention, stepped onto the Java way of combat ! ! !
Java combat road
Author: how to change
copyright of all, please indicate the source, welcome to reprint

Guess you like

Origin www.cnblogs.com/theonesmx/p/12498947.html