问题描述
我在Client类中使用JsonNode处理MySQL 8数据库中具有JSON类型的字段.即使使用API请求,它也可以很好地工作.但是,当我启用Redis缓存(我确实需要它)时,我注意到Redis无法序列化JsonNode.我在互联网上搜索以更改Redis的序列化方法.并提出了以下代码:
I use JsonNode in my Client class to deal with a field with JSON type in MySQL 8 database. It works very well even with API requests. But when I enable caching with Redis (which I really need it), I notice that Redis is not able to serialize JsonNode. I searched the internet for changing the serialization method of Redis. and came up with the following code:
@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,但是无法反序列化它们!并产生以下错误:
It serializes my objects successfully and put them in Redis successfully, but cannot to deserialize them!. And produces the following error:
这是我的客户班:
@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时会引发异常.
I noticed tht the error is not related to JsonNode, It throws the exception when I use GenericJackson2JsonRedisSerializer.
推荐答案
我遇到了非常相似的问题,解决方案很奇怪,但是很简单-删除 devtools
依赖项.
I had very similar problem and the solution was strange, but simple - remove devtools
dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
这篇关于在Spring Boot中反序列化Redis缓存到对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!