我希望有人能提供帮助,因为我真的不知道如何解决以下问题:我希望用Spring Data RedisineCrudRepository
序列化Entity
和ThumbnailurlEntity
(请参见下面的代码)的一些对象。因为类中有我不想序列化的成员,所以我尝试以各种方式忽略它们,使用@jsonignoreproperties
注释类本身,或者使用@jsonignore
注释相应的成员/属性,或者使用redisconfig.java
中的customconversion
注释filters
注释相应的成员/属性。
现在的问题是,无论我尝试哪种忽略方式,我总是得到相同的错误org.springframework.data.keyvalue.core.uncategorizedKeyValueException:property的路径不能为null或空;嵌套异常是java.lang.IllegalArgumentException:当我试图保存相应的对象时,Path to property不能为null或空。
。(有关更多细节,请参见下面的Stacktrace)
我甚至在那些课上用Mixin的试过,但效果不太好...
那么问题是:当用spring-data-redis序列化某个类的对象时,如何正确地忽略属性呢?
@EqualsAndHashCode(exclude = "domainObject")
@ToString(exclude = "domainObject")
@NoArgsConstructor
//@JsonIgnoreProperties("domainObject")
@JsonFilter("myEntityFilter")
public abstract class Entity<T extends DomainObject> {
@TimeToLive
protected final static long TIME_TO_LIVE = 60 * 60 * 24;
@Id
@Indexed
private String id;
// @Transient
// @JsonIgnore
private T domainObject;
public Entity(@NotNull T domainObject) {
this.domainObject = domainObject;
}
public abstract Entity<T> createFromDomainObject(@NotNull T domainObject);
public abstract void updateFromDomainObject(T domainObject);
public void updateFromDomainObject() {
this.updateFromDomainObject(this.getDomainObject());
}
public String getId() {
return id;
}
// @Transient
// @JsonIgnore
public T getDomainObject() {
return domainObject;
}
public void setDomainObject(T domainObject) {
this.domainObject = domainObject;
}
}
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false, exclude = "parentList")
@ToString(exclude = "parentList")
@RedisHash("thumbnail_url_entity")
//@JsonIgnoreProperties("parentList")
@JsonFilter("myThumbnailUrlEntityFilter")
public class ThumbnailUrlEntity extends Entity<ThumbnailUrl> {
@Indexed
private String url;
private Integer priority;
// @Transient
// @JsonIgnore
private ThumbnailUrlListEntity parentList;
public ThumbnailUrlEntity(@NotNull ThumbnailUrl domainObject) {
super(domainObject);
this.updateFromDomainObject(domainObject);
}
@Override
public Entity<ThumbnailUrl> createFromDomainObject(@NotNull ThumbnailUrl domainObject) {
ThumbnailUrlEntity entity = new ThumbnailUrlEntity();
entity.url = domainObject.getUrl();
entity.priority = domainObject.getPriority();
return entity;
}
@Override
public void updateFromDomainObject(@NotNull ThumbnailUrl domainObject) {
this.url = domainObject.getUrl();
this.priority = domainObject.getPriority();
}
public String getUrl() {
return url;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
// @Transient
// @JsonIgnore
public ThumbnailUrlListEntity getParentList() {
return parentList;
}
public void setParentList(ThumbnailUrlListEntity parentList) {
this.parentList = parentList;
}
}
@Configuration
@EnableRedisRepositories(basePackages = "nlp.floschne.thumbnailAnnotator.db")
public class RedisConfig {
@Bean
RedisConnectionFactory connectionFactory() {
return new LettuceConnectionFactory();
}
@Bean
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setKeySerializer(new StringRedisSerializer());
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_EMPTY);
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("domainObject");
FilterProvider filters = new SimpleFilterProvider()
.addFilter("myEntityFilter", theFilter)
.addFilter("myThumbnailUrlEntityFilter", theFilter);
mapper.setFilters(filters);
template.setValueSerializer(new GenericJackson2JsonRedisSerializer(mapper));
return template;
}
@Bean
public RedisCustomConversions redisCustomConversions() {
return new RedisCustomConversions(Arrays.asList(
new EntityToBytesConverter(),
new BytesToEntityConverter(),
new ThumbnailUrlEntityToBytesConverter(),
new BytesToThumbnailUrlEntityConverter()));
}
@WritingConverter
public class EntityToBytesConverter implements Converter<Entity<?>, byte[]> {
private final GenericJackson2JsonRedisSerializer serializer;
public EntityToBytesConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_EMPTY);
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("domainObject");
FilterProvider filters = new SimpleFilterProvider().addFilter("myEntityFilter", theFilter);
mapper.setFilters(filters);
serializer = new GenericJackson2JsonRedisSerializer(mapper);
}
@Override
public byte[] convert(Entity value) {
return serializer.serialize(value);
}
}
@ReadingConverter
public class BytesToEntityConverter implements Converter<byte[], Entity<?>> {
private final GenericJackson2JsonRedisSerializer serializer;
public BytesToEntityConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_EMPTY);
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("domainObject");
FilterProvider filters = new SimpleFilterProvider().addFilter("myEntityFilter", theFilter);
mapper.setFilters(filters);
serializer = new GenericJackson2JsonRedisSerializer(mapper);
}
@Override
public Entity convert(byte[] value) {
return (Entity) serializer.deserialize(value);
}
}
@WritingConverter
public class ThumbnailUrlEntityToBytesConverter implements Converter<ThumbnailUrlEntity, byte[]> {
private final GenericJackson2JsonRedisSerializer serializer;
public ThumbnailUrlEntityToBytesConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_EMPTY);
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("parentList");
FilterProvider filters = new SimpleFilterProvider().addFilter("myThumbnailUrlEntityFilter", theFilter);
mapper.setFilters(filters);
serializer = new GenericJackson2JsonRedisSerializer(mapper);
}
@Override
public byte[] convert(ThumbnailUrlEntity value) {
return serializer.serialize(value);
}
}
@ReadingConverter
public class BytesToThumbnailUrlEntityConverter implements Converter<byte[], ThumbnailUrlEntity> {
private final GenericJackson2JsonRedisSerializer serializer;
public BytesToThumbnailUrlEntityConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_EMPTY);
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("parentList");
FilterProvider filters = new SimpleFilterProvider().addFilter("myThumbnailUrlEntityFilter", theFilter);
mapper.setFilters(filters);
serializer = new GenericJackson2JsonRedisSerializer(mapper);
}
@Override
public ThumbnailUrlEntity convert(byte[] value) {
return (ThumbnailUrlEntity) serializer.deserialize(value);
}
}
}
public interface ThumbnailUrlEntityRepository extends CrudRepository<ThumbnailUrlEntity, String> {
}
org.springframework.data.keyvalue.core.UncategorizedKeyValueException: Path to property must not be null or empty.; nested exception is java.lang.IllegalArgumentException: Path to property must not be null or empty.
at org.springframework.data.keyvalue.core.KeyValuePersistenceExceptionTranslator.translateExceptionIfPossible(KeyValuePersistenceExceptionTranslator.java:55)
at org.springframework.data.keyvalue.core.KeyValueTemplate.resolveExceptionIfPossible(KeyValueTemplate.java:459)
at org.springframework.data.keyvalue.core.KeyValueTemplate.execute(KeyValueTemplate.java:345)
at org.springframework.data.keyvalue.core.KeyValueTemplate.insert(KeyValueTemplate.java:158)
at org.springframework.data.redis.core.RedisKeyValueTemplate.insert(RedisKeyValueTemplate.java:125)
at org.springframework.data.keyvalue.core.KeyValueTemplate.insert(KeyValueTemplate.java:140)
at org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository.save(SimpleKeyValueRepository.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:629)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:593)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy43.save(Unknown Source)
at nlp.floschne.thumbnailAnnotator.db.repository.ThumbnailUrlEntityRepositoryTests.whenSaving_thenAvailableOnRetrieval(ThumbnailUrlEntityRepositoryTests.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: Path to property must not be null or empty.
at org.springframework.util.Assert.hasText(Assert.java:276)
at org.springframework.data.redis.core.convert.Bucket.put(Bucket.java:72)
at org.springframework.data.redis.core.convert.MappingRedisConverter.writeToBucket(MappingRedisConverter.java:750)
at org.springframework.data.redis.core.convert.MappingRedisConverter.writeInternal(MappingRedisConverter.java:571)
at org.springframework.data.redis.core.convert.MappingRedisConverter.write(MappingRedisConverter.java:396)
at org.springframework.data.redis.core.convert.MappingRedisConverter.write(MappingRedisConverter.java:122)
at org.springframework.data.redis.core.RedisKeyValueAdapter.put(RedisKeyValueAdapter.java:208)
at org.springframework.data.keyvalue.core.KeyValueTemplate.lambda$insert$0(KeyValueTemplate.java:165)
at org.springframework.data.keyvalue.core.KeyValueTemplate.execute(KeyValueTemplate.java:343)
... 58 more
entityJsonSerializer.java
public class EntityJsonSerializer implements RedisSerializer<Entity> {
private final ObjectMapper mapper;
public EntityJsonSerializer() {
this.mapper = new ObjectMapper();
this.mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
this.mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
this.mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
this.mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
this.mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_EMPTY);
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("domainObject", "parentList");
FilterProvider filters = new SimpleFilterProvider()
.addFilter("myEntityFilter", theFilter)
.addFilter("myThumbnailUrlEntityFilter", theFilter);
this.mapper.setFilters(filters);
}
public EntityJsonSerializer(ObjectMapper mapper) {
this.mapper = mapper;
}
@Override
public byte[] serialize(Entity t) throws SerializationException {
try {
return mapper.writeValueAsBytes(t);
} catch (JsonProcessingException e) {
throw new SerializationException(e.getMessage(), e);
}
}
@Override
public Entity deserialize(byte[] bytes) throws SerializationException {
if (bytes == null) {
return null;
}
try {
return mapper.readValue(bytes, Entity.class);
} catch (Exception e) {
throw new SerializationException(e.getMessage(), e);
}
}
}
更新的redisconfig.java
@Configuration
@EnableRedisRepositories(basePackages = "nlp.floschne.thumbnailAnnotator.db")
public class RedisConfig {
@Bean
RedisConnectionFactory connectionFactory() {
return new LettuceConnectionFactory();
}
@Bean
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new EntityJsonSerializer());
return template;
}
@Bean
public RedisCustomConversions redisCustomConversions() {
return new RedisCustomConversions(Arrays.asList(
new EntityToBytesConverter(),
new BytesToEntityConverter(),
new ThumbnailUrlEntityToBytesConverter(),
new BytesToThumbnailUrlEntityConverter()));
}
@WritingConverter
public class EntityToBytesConverter implements Converter<Entity<?>, byte[]> {
private final EntityJsonSerializer serializer;
public EntityToBytesConverter() {
serializer = new EntityJsonSerializer();
}
@Override
public byte[] convert(Entity value) {
return serializer.serialize(value);
}
}
@ReadingConverter
public class BytesToEntityConverter implements Converter<byte[], Entity<?>> {
private final EntityJsonSerializer serializer;
public BytesToEntityConverter() {
serializer = new EntityJsonSerializer();
}
@Override
public Entity convert(byte[] value) {
return (Entity) serializer.deserialize(value);
}
}
@WritingConverter
public class ThumbnailUrlEntityToBytesConverter implements Converter<ThumbnailUrlEntity, byte[]> {
private final EntityJsonSerializer serializer;
public ThumbnailUrlEntityToBytesConverter() {
serializer = new EntityJsonSerializer();
}
@Override
public byte[] convert(ThumbnailUrlEntity value) {
return serializer.serialize(value);
}
}
@ReadingConverter
public class BytesToThumbnailUrlEntityConverter implements Converter<byte[], ThumbnailUrlEntity> {
private final EntityJsonSerializer serializer;
public BytesToThumbnailUrlEntityConverter() {
serializer = new EntityJsonSerializer();
}
@Override
public ThumbnailUrlEntity convert(byte[] value) {
return (ThumbnailUrlEntity) serializer.deserialize(value);
}
}
}
我认为你可以做以下其中之一:
首先,你可以尝试得到当前版本的工作,这似乎并不容易。
您是否为Redis配置了正确的序列化程序?在链接上有这样的答案:
问题内容: 我有一些扩展的模型类:它们定义用于包装Map的get和put方法的getter和setter。我正在尝试使用Jackson(带有RESTEasy)序列化这些类的实例,但是Jackson拒绝注意我的getter(使用注释)。而是仅序列化支持映射的键- 值对。我尝试使用禁用所有方法和字段的自动检测功能,但这并没有任何改变。有没有办法防止Jackson序列化Map,还是必须创建不扩展的新模型
问题内容: 我想序列化一个不受我控制的POJO类,但是想避免序列化任何来自超类而不是最终类的属性。例: 您可以从示例中猜测到,该类是由JOOQ生成的,并且是从复杂的基类UpdatableRecordImpl继承的,该基类还具有一些类似于bean属性的方法,这会在序列化过程中引起问题。另外,我有几个类似的类,因此最好避免对所有生成的POJO复制相同的解决方案。 到目前为止,我已经找到以下可能的解决方
> 使用mixin技术忽略来自超类的特定字段,如下所示:我如何告诉jackson忽略一个我无法控制源代码的属性? 这样做的问题是,如果基类发生了变化(例如,其中出现了一个新的getAnything()方法),它可能会破坏我的实现。 实现一个自定义序列化程序并在那里处理问题。这在我看来有点矫枉过正。 但是,从纯设计的角度来看,最好的方法是告诉jackson我只想序列化最终类的属性,而忽略所有继承的属
问题内容: 我使用以下代码来序列化从外部服务获得的响应,并作为我的服务的一部分返回json响应。但是,当外部服务返回带有时区(10:30:00.000-05.00)的时间值时,杰克逊会将其转换为15:30:00。如何忽略时区值? 问题答案: 您可以创建自定义解串器 告诉杰克逊使用您的自定义解串器 并像这样使用它: 您可以使用Jackson自定义序列化为服务响应添加时区信息
我正在尝试使用spring data redis的Jackson序列化功能。我正在构建一个ObjectMapper,并使用GenericJackson2JsonRedisSerializer作为redisTemplate的序列化程序: 我正试图保存一个样本bean: 以及该bean的存储库: 然后我尝试将bean写入Redis: 我希望redisTemplate使用序列化程序将SampleBean
我的Nifi处理器实现生成了一个奇怪的缓存键和值,类似于'\xac\xed\x00\x05t\x00\x06'的内容被前置到每个键/val。例如,插入'key':'val'生成“\xac\xed\x00\x05t\x00\x03key”:“\xac\xed\x00\x05t\x00\x03val”。 我没有spring配置可用于编辑或spring数据类,这都是在NIFI的实现中完成的。我所拥有和必