Springbean覆盖我的ObjectMapper配置
今天出现了一个问题,当涉及到我试图进行的Springbean覆盖我的ObjectMapper配置,spring,spring-boot,objectmapper,Spring,Spring Boot,Objectmapper,今天出现了一个问题,当涉及到我试图进行的ObjectMapper配置时,一个与配置关联的依赖项一直在获胜。我将以下内容添加到我的Spring Boot应用程序中 @Configuration public class CustomObjectMapperConfig { @Autowired public void configureObjectMapper(ObjectMapper objectMapper) { objectMapper.enable(Ser
ObjectMapper
配置时,一个与配置关联的依赖项一直在获胜。我将以下内容添加到我的Spring Boot应用程序中
@Configuration
public class CustomObjectMapperConfig {
@Autowired
public void configureObjectMapper(ObjectMapper objectMapper) {
objectMapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
}
实际上我不想使用时间戳,但为了向后兼容,我不得不这样做。我在这里的问题行上放了一个调试点,它被击中了,但我一直以ISO格式将日期返回给我,这是大多数项目或我们项目的默认格式。我最终发现,我引入的公司依赖项具有以下特征:
@Configuration
public class ObjectMapperPropertiesConfig {
/**
* @deprecated Spring boot jackson properties should be used instead.
*/
@Deprecated
@Autowired
public void setObjectMapper(
final ObjectMapper objectMapper) {
objectMapper
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, !ignoreUnknown);
}
...
这个类是在我的类之后加载的,所以它在WRITE\u DATES\u AS\u TIMESTAMPS
特性方面一直是赢家
为了解决这个问题,我添加了一个@DependsOn
注释。这迫使另一个bean首先加载,从而使我的bean有机会赢得配置战。很难找到bean的正确名称。它最终看起来像这样:
@DependsOn("path.to.object.ObjectMapperPropertiesConfig")
注意:这里的@Deprecated
注释告诉我,在将来的版本中,该代码将消失,而支持spring引导属性。现在,我的改变将起作用