在 Spring Boot 应用程序中从缓存获取期间使用 @AutoValue 注释反序列化 class
Deserialize class with @AutoValue annotation during getting from cache in SpringBoot application
我创建了一个带有 com.google.auto.value.AutoValue
注释的值 class:
@AutoValue
@JsonDeserialize(builder = AutoValue_Company.Builder.class)
public abstract class Company {
public static Builder newBuilder() {
return new AutoValue_Company.Builder();
}
@JsonProperty("id")
public abstract long id();
@JsonProperty("description")
public abstract String description();
@JsonProperty("websiteUrl")
public abstract String websiteUrl();
@AutoValue.Builder
public interface Builder {
@JsonProperty("id")
Builder id(long id);
@JsonProperty("description")
Builder description(String description);
@JsonProperty("websiteUrl")
Builder websiteUrl(String url);
Company build();
}
}
从服务接收到此类 class 的实例并将结果缓存在 Redis 中:
@Cacheable(value = "company")
public Company getValue(String id)
这是 RedisCacheManager 的配置:
@Bean(name = "valueCacheManager")
public RedisCacheManager valueCacheManager(final RedisConnectionFactory connectionFactory) {
final RedisCacheWriter redisCacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(100));
return new RedisCacheManager(redisCacheWriter, cacheConfiguration);
}
因此与反序列化相关的问题:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.test.models.AutoValue_Company and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
也许您应该使用 getters 约定命名您的方法,例如getId()
等等?我认为 this 是相关的。
通过将序列化程序 GenericJackson2JsonRedisSerializer
更改为 Jackson2JsonRedisSerializer<>(Company.class)
找到了解决方案
所以缓存配置现在看起来像这样:
@Bean(name = "valueCacheManager")
public RedisCacheManager valueCacheManager(final RedisConnectionFactory connectionFactory) {
final RedisCacheWriter redisCacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<>(Company.class)));
cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(100));
return new RedisCacheManager(redisCacheWriter, cacheConfiguration);
}
我创建了一个带有 com.google.auto.value.AutoValue
注释的值 class:
@AutoValue
@JsonDeserialize(builder = AutoValue_Company.Builder.class)
public abstract class Company {
public static Builder newBuilder() {
return new AutoValue_Company.Builder();
}
@JsonProperty("id")
public abstract long id();
@JsonProperty("description")
public abstract String description();
@JsonProperty("websiteUrl")
public abstract String websiteUrl();
@AutoValue.Builder
public interface Builder {
@JsonProperty("id")
Builder id(long id);
@JsonProperty("description")
Builder description(String description);
@JsonProperty("websiteUrl")
Builder websiteUrl(String url);
Company build();
}
}
从服务接收到此类 class 的实例并将结果缓存在 Redis 中:
@Cacheable(value = "company")
public Company getValue(String id)
这是 RedisCacheManager 的配置:
@Bean(name = "valueCacheManager")
public RedisCacheManager valueCacheManager(final RedisConnectionFactory connectionFactory) {
final RedisCacheWriter redisCacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(100));
return new RedisCacheManager(redisCacheWriter, cacheConfiguration);
}
因此与反序列化相关的问题:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.test.models.AutoValue_Company and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
也许您应该使用 getters 约定命名您的方法,例如getId()
等等?我认为 this 是相关的。
通过将序列化程序 GenericJackson2JsonRedisSerializer
更改为 Jackson2JsonRedisSerializer<>(Company.class)
所以缓存配置现在看起来像这样:
@Bean(name = "valueCacheManager")
public RedisCacheManager valueCacheManager(final RedisConnectionFactory connectionFactory) {
final RedisCacheWriter redisCacheWriter = RedisCacheWriter.lockingRedisCacheWriter(connectionFactory);
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<>(Company.class)));
cacheConfiguration = cacheConfiguration.entryTtl(Duration.ofSeconds(100));
return new RedisCacheManager(redisCacheWriter, cacheConfiguration);
}