Java 如何在spring boot中注册自定义转换器?
我使用SpringBootStarterJDBC(v1.3.0)编写应用程序 我遇到的问题是:Java 如何在spring boot中注册自定义转换器?,java,spring,spring-boot,spring-data,Java,Spring,Spring Boot,Spring Data,我使用SpringBootStarterJDBC(v1.3.0)编写应用程序 我遇到的问题是:beanpropertyrowapper的实例失败,因为它无法从java.sql.Timestamp转换为java.time.LocalDateTime 为了复制这个问题,我实现了 org.springframework.core.convert.converter.converter用于这些类型 public class TimeStampToLocalDateTimeConverter implem
beanpropertyrowapper
的实例失败,因为它无法从java.sql.Timestamp
转换为java.time.LocalDateTime
为了复制这个问题,我实现了
org.springframework.core.convert.converter.converter
用于这些类型
public class TimeStampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> {
@Override
public LocalDateTime convert(Timestamp s) {
return s.toLocalDateTime();
}
}
谢谢。我建议使用@Autowired和spring的相关依赖注入机制,在整个应用程序中使用单个ConversionService实例。ConversionService将在配置中实例化 所有在应用范围内可用的转换器都会收到注释(例如@AutoRegistered)。在应用程序启动时,@Component FormatterRegistrar(类型名称本身有点误导,是的,它是“…注册器”,因为它进行注册。而@Component是完全spring管理的,需要依赖项注入)将收到所有带注释转换器的@AutoRegistered列表 查看此线程以了解详细信息。我们在项目中使用这种机制,它的效果非常好 正在尝试添加
@Converter(autoApply = true)
它需要置于convertor类之上。这在Localdate与DB交互所需的转换器的情况下适用
@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate locDate) {
return (locDate == null ? null : Date.valueOf(locDate));
}
@Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
return (sqlDate == null ? null : sqlDate.toLocalDate());
}
}
@转换器(autoApply=true)
公共类LocalDateAttributeConverter实现AttributeConverter{
@凌驾
public Date convertToDatabaseColumn(LocalDate locDate){
返回(locDate==null?null:Date.valueOf(locDate));
}
@凌驾
公共LocalDate convertToEntityAttribute(日期sqlDate){
返回(sqlDate==null?null:sqlDate.toLocalDate());
}
}
现在,在与DB交互时,会自动应用此功能。
org.springframework.web.servlet.config.annotation.WebMVCConfiguer
或其任何实现都是spring boot项目中任何类型自定义的一站式位置。根据您的转换器要求,它可以避免各种方法
只需通过扩展org.springframework.core.convert.Converter.Converter
来创建一个新的转换器。然后通过类重写方法org.springframework.web.servlet.config.annotation.webmvcconfiguer.addFormatters(FormatterRegistry)
注意,还有其他类型的转换器,基本上是从ConditionalConverter开始的。所有自定义转换服务都必须向FormatterRegistry注册。尝试创建新配置,并通过实现WebMVCConfiguer注册转换服务
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addallFormatters(FormatterRegistry registry) {
registry.addConverter(new TimeStampToLocalDateTimeConverter());
}
}
希望这能奏效 只需将转换器添加为bean。。。删除所有其他内容。在此处混淆
HttpMessageConverter
和Converter
。只需添加一个扩展WebMVCConfigureAdapter
的类,并实现addFormatters
方法。在FormatterRegistry
上,调用addConverter
,查找您要添加的内容。我根本没有web环境。那么它确实无法工作:)。但是您拥有的代码应该可以工作,您是否可以添加完整的配置类和使用此功能时得到的错误(堆栈跟踪)…为了保存一些代码,我不会调用AfterPropertieSet()
和getObject()
。相反,让Spring为您做这件事,只需返回工厂bean即可。问题是关于Spring(核心)转换器,而不是JPA转换器。谢谢。但是方法名应该是addFormatters()```
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addallFormatters(FormatterRegistry registry) {
registry.addConverter(new TimeStampToLocalDateTimeConverter());
}
}