扩展RepositoryRestMvcConfiguration会破坏Jackson LocalDateTime序列化 [英] Extending RepositoryRestMvcConfiguration breaks Jackson LocalDateTime Serialisation
问题描述
我正在尝试扩展 RepositoryRestMvcConfiguration
以覆盖自定义的 uriToEntityConverter
,但这样做会导致杰克逊无法从 String
序列化/反序列化 LocalDateTime
。下面的代码显示了我的子类。
I'm attempting to extend RepositoryRestMvcConfiguration
to override the uriToEntityConverter
for a custom one, however doing that then causes Jackson to fail to be able to serialise/deserialise LocalDateTime
from String
. The code below shows my subclass.
我也尝试将 jackson-datatype-jsr310
添加到 pom
看看我是否可以强制支持,但是没有我的子类它无论如何都可以工作,所以这主要是一个死胡同。
I've also tried adding jackson-datatype-jsr310
into the pom
to see if I can force support, but without my subclass it works anyway so that was mostly a dead-end.
@Configuration
@Import(RepositoryRestMvcConfiguration.class)
public class RepositoryRestMvcConfigurer extends RepositoryRestMvcConfiguration {
@Override
@ConfigurationProperties(prefix = "spring.data.rest")
public RepositoryRestConfiguration config() {
return super.config();
}
@Override
protected UriToEntityConverter uriToEntityConverter(ConversionService conversionService) {
return new OverriddenUriToEntityConverter(persistentEntities(), repositoryInvokerFactory(conversionService), repositories());
}
}
编辑:
我已经解决了通过注释我的 LocalDateTime
属性来指定要使用的序列化器和反序列化器的问题,但这更像是一个伏都教仪式而非解决方案。
I've solved the problem by annotating my LocalDateTime
properties to specify what serialiser and deserialiser to use, but that feels more like a voodoo ritual than a solution.
推荐答案
所以我最终自我解决了这个问题。要实际扩展你还需要:
So I ended up self-solving this. To actually extended that you also need:
- 扩展
RepositoryRestConfigurerAdapter
- 返回上一个类并且还实现
AutoConfigureAfter
- A class that extends
RepositoryRestConfigurerAdapter
- A class that returns that previous class and also implements the annotations for
AutoConfigureAfter
我基于 Spring 中的 RepositoryRestMvcAutoConfiguration
类,但是将其改为 @ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class)
到 @ConditionalOnBean(ExtendedRepositoryRestMvcConfiguration.class)
。这允许它使用重写的函数正确配置 RestRepository
。
I based mine on the RepositoryRestMvcAutoConfiguration
class actually within Spring but changed it from being @ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class)
to @ConditionalOnBean(ExtendedRepositoryRestMvcConfiguration.class)
. That allowed it to properly configure the RestRepository
with my overridden functions.
这篇关于扩展RepositoryRestMvcConfiguration会破坏Jackson LocalDateTime序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!