首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring中将redis-cache反序列化为对象的问题

在Spring中将redis-cache反序列化为对象的问题
EN

Stack Overflow用户
提问于 2019-09-08 15:55:44
回答 2查看 3.9K关注 0票数 5

我在客户端类中使用JsonNode来处理MySQL 8数据库中具有JSON类型的字段。即使是在API请求下,它也运行得很好。但是当我使用Redis启用缓存时(我确实需要它),我注意到Redis无法序列化JsonNode。我在网上搜索以改变Redis的序列化方法。并提出了以下代码:

代码语言:javascript
复制
@Configuration
@EnableCaching
public class RedisConfig {

    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        return Jackson2ObjectMapperBuilder.json()
                .serializationInclusion(JsonInclude.Include.NON_NULL) // Don’t include null values
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) //ISODate
                .build();
    }

    @Bean
    public RedisTemplate getRedisTemplate(ObjectMapper objectMapper, RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

    @Bean
    @Primary
    public RedisCacheConfiguration defaultCacheConfig(ObjectMapper objectMapper) {
        return RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer(objectMapper)));
    }

}

它成功地序列化了我的对象,并成功地将它们放到Redis中,但是不能反序列化它们!并产生以下错误:

org.springframework.boot.devtools.restart.classloader.RestartClassLoader : java.lang.ClassCastException:不能将类java.util.LinkedHashMap转换为com.example.Client类(java.util.LinkedHashMap位于加载程序‘引导’的模块java.base中;com.example.Client位于加载程序的未命名模块com.example.Client@4683d900中)]

这是我的客户类:

代码语言:javascript
复制
@Data
@Entity
@Table( name = "clients" )
@EntityListeners(AuditingEntityListener.class)
@TypeDef(name = "json", typeClass = JsonStringType.class)
public class Client  {
    /**
     * Id of the user
     */
    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY )
    private Long id;
    /**
     * UUID of the user.
     */
    @Column( name = "uuid", unique = true, updatable = false, nullable = false )
    private String uuid;


    /**
     * Name of the client
     */
    @Column( name = "name", unique = true, nullable = false )
    @Size( min=4, max=128, message = "client.exception.name.size" )
    @NotBlank( message = "client.exception.name.isBlank" )
    private String name;


    /**
     * Status of the client. Each client could have several statues.
     * 1.
     */
    @Column( name = "client_status_id" )
    @NotNull( message = "client.exception.status.isNull" )
    @Enumerated( EnumType.ORDINAL )
    private ClientStatus clientStatus = ClientStatus.Undefined;


    /**
     * Email address of the client.
     */
    @Column( name = "email" )
    @NotBlank( message = "client.exception.email.isBlank")
    @Size( min=5, max = 128, message = "client.exception.email.size")
    @Email( message = "client.exception.email.notValid" )
    private String email;

    /**
     * ClientNotFoundByIdException's phone number.
     */
    @Column( name = "phone" )
    @NotBlank( message = "client.exception.phone.isBlank" )
    @Size( min=3, max = 32, message = "client.exception.phone.size")
    @Phone( message = "client.exception.phone.notValid" )
    private String phone;

    /**
     * Whether client is active or not.
     */
    @Column( name = "is_active" )
    private Boolean isActive = true;

    /**
     * Default timezone of the client.
     */
    @Column( name = "timezone" )
    @NotBlank( message = "client.exception.timezone.isBlank" )
    @Size( min = 4, max = 64, message = "client.exception.timezone.size" )
    @TimeZone( message = "client.exception.timezone.notValid" )
    private String timezone;


    /**
     * Country code of the client in ISO 3166-2
     */
    @Column( name = "country" )
    @NotBlank( message = "client.exception.country.isBlank" )
    @Size( min = 2, max = 4, message = "client.exception.country.size" )
    @Country( message = "client.exception.country.notValid" )
    private String country;


    @Column( name = "language" )
    @NotBlank( message = "client.exception.language.isBlank" )
    @Size( min = 2, max = 3, message = "client.exception.language.size" )
    @Language
    private String language;


    /**
     * Extra fields for client in json
     */
    @Column( name = "fields", columnDefinition = "json" )
    @Type( type = "json" )
    @NotNull( message = "client.exception.fields.isNull" )
    private JsonNode fields;


    /**
     * Creation time of the record.
     */
    @Column( name = "created_at", updatable = false )
    @NotNull( message = "client.exception.createdAt.isNull")
    @CreatedDate
    private Instant createdAt;


    /**
     * The user that created the record.
     */
    @CreatedBy
    @ManyToOne
    @JoinColumn( name = "created_by" )
    private User createdBy;


    /**
     * In which time the record is modified.
     */
    @Column( name = "updated_at" )
    @NotNull( message = "client.exception.updatedAt.isNull" )
    @LastModifiedDate
    private Instant updatedAt;


    /**
     * By whom the record is modified.
     */
    @LastModifiedBy
    @ManyToOne
    @JoinColumn( name = "updated_by" )
    private User updatedBy;


    /**
     * The time in which the record is deleted.
     */
    private Instant deletedAt;


    /**
     * By whom the record is deleted.
     */
    @ManyToOne
    @JoinColumn( name = "deleted_by" )
    private User deleteBy;


    /**
     * Version of the record.
     */
    @Version
    private Long version;
}

更新:

我注意到这个错误与JsonNode无关,当我使用GenericJackson2JsonRedisSerializer时它会抛出异常。

EN

回答 2

Stack Overflow用户

发布于 2020-10-28 11:08:08

我遇到了非常类似的问题,解决方案很奇怪,但很简单--删除devtools依赖项。

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
票数 2
EN

Stack Overflow用户

发布于 2022-07-01 10:25:18

通过定义自定义的ReadingConverterWritingConverter来解决这个问题。有了这两种方法,我就能够成功地反序列化JsonNode类型的属性。

代码语言:javascript
复制
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.convert.converter.Converter
import org.springframework.data.convert.ReadingConverter
import org.springframework.data.convert.WritingConverter
import org.springframework.data.redis.core.convert.RedisCustomConversions
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
import org.springframework.stereotype.Component

@WritingConverter
@Component
class JsonNodeToByteArrayConverter() : Converter<JsonNode, ByteArray> {

        private var serializer: Jackson2JsonRedisSerializer<JsonNode> = Jackson2JsonRedisSerializer(JsonNode::class.java)

    init {
        serializer.setObjectMapper(ObjectMapper())
    }

    override fun convert(source: JsonNode): ByteArray {
        return serializer.serialize(source)
    }
}

@ReadingConverter
@Component
class ByteArrayToJsonNodeConverter : Converter<ByteArray, JsonNode> {

        private var serializer: Jackson2JsonRedisSerializer<JsonNode> = Jackson2JsonRedisSerializer(JsonNode::class.java)

    init {
        serializer.setObjectMapper(ObjectMapper())
    }

    override fun convert(source: ByteArray): JsonNode {
        return serializer.deserialize(source)
    }
}

/**
 * Custom mappers are needed to convert JsonNode (otherwise, deserialization fails)
 */
@Configuration
@EnableRedisRepositories
class RedisConfiguration {
    @Bean
    fun redisCustomConversions(
        byteArrayToJsonNodeConverter: ByteArrayToJsonNodeConverter,
        jsonNodeToByteArrayConverter: JsonNodeToByteArrayConverter
    ): RedisCustomConversions {
        return RedisCustomConversions(listOf(byteArrayToJsonNodeConverter, jsonNodeToByteArrayConverter))
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57843483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档