我有一个用户定义的类,如下(常规代码):
<Image
source={{uri:rowData.article_image}}
/>
有一个User类:
@CompileStatic
@JsonSerialize(using = JsonSerializer)
@JsonDeserialize(using = JsonDeserializer)
class SensitiveString implements Serializabl{
private String rawString
SensitiveString(String raw) {
rawString = raw
}
Object asType(Class clazz) {
assert clazz == String: "Cannot cast Sensitive String to type other than string. "
return this.toString()
}
@Override
String toString() {
return rawString
}
boolean equals(SensitiveString other){
return other.rawString == rawString
}
static class JsonSerializer extends StdSerializer<SensitiveString> {
JsonSerializer() {
super(SensitiveString.class)
}
@Override
void serialize(SensitiveString value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (value == null || value.rawString == null) {
gen.writeNull()
return
}
gen.writeString(SensitiveDataGuard.INSTANCE.encryptData(value.rawString))
}
}
static class JsonDeserializer extends StdDeserializer<SensitiveString> {
JsonDeserializer() {
super(SensitiveString.class)
}
@Override
SensitiveString deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
String encrypted = p.readValueAs(String)
if (encrypted == null)
return null
return new SensitiveString(SensitiveDataGuard.INSTANCE.decryptData(encrypted))
}
}
}
我在项目中使用Mybatis,在UserMapper中有一个listUsersBy方法,调用此方法时,出现以下异常:
[未为类型mypackage.SensitiveString实现类型ID处理(通过类型为mypackage.SensitiveString $ JsonSerializer的序列化器)(通过参考链:java.util.ArrayList [0]-> mypackage.User [“ passed”])]
通过深入研究问题,我发现这是由于二级缓存处理中的序列化。我将Redis与GenericJackson2JsonRedisSerializer的JSON解析器一起用于Mybatis的二级缓存。
class User implements Serializable{
String name;
SensitiveString passed;
...
}
当调用listUsersBy()时,将 redisTemplate.setKeySerializer(new StringRedisSerializer())
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer())
redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer())
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer())
进行序列化,但是序列化程序无法对其进行序列化,因为列表的每个元素都具有用户定义的类SensitiveString,该类应该有问题带有其序列化程序。
我只大致知道问题可能在哪里,但我不知道确切原因以及如何解决。谁能提供一些线索或解决方法?