在实体类java.time.ZonedDateTime上未找到要将构造函数参数绑定到的属性null
我有以下配置 配置 pom.xml在实体类java.time.ZonedDateTime上未找到要将构造函数参数绑定到的属性null,java,spring-data,jhipster,spring-data-mongodb,spring-mongodb,Java,Spring Data,Jhipster,Spring Data Mongodb,Spring Mongodb,我有以下配置 配置 pom.xml org.springframework.boot spring启动程序父级 1.5.1.1发布 org.springframework.boot spring启动程序数据mongodb 转换器类 公共最终类JSRConverter{ 私有JSRConverters(){} 公共静态类ZonedDateTimeToDateConverter实现转换器{ 公共静态最终ZonedDateTimeToDateConverter实例=新的ZonedDateTimeT
org.springframework.boot
spring启动程序父级
1.5.1.1发布
org.springframework.boot
spring启动程序数据mongodb
转换器类
公共最终类JSRConverter{
私有JSRConverters(){}
公共静态类ZonedDateTimeToDateConverter实现转换器{
公共静态最终ZonedDateTimeToDateConverter实例=新的ZonedDateTimeToDateConverter();
私有ZonedDateTimeToDateConverter(){}
@凌驾
公共日期转换(最终ZonedDateTime源){
返回源==null?null:Date.from(source.toInstant());
}
}
公共静态类DateToZonedDateTimeConverter实现转换器{
公共静态最终DateToZonedDateTimeConverter实例=新建DateToZonedDateTimeConverter();
专用DateToZonedDateTimeConverter(){}
@凌驾
公共分区日期时间转换(最终日期源){
返回源==null?null:ZonedDateTime.ofInstant(source.toInstant(),ZoneId.systemDefault());
}
}
}
我有相同的设置,但使用org.springframework.data.mongodb.core.convert.MongoCustomConversions而不是org.springframework.data.mongodb.core.convert.自定义转换
@Bean
public MongoCustomConversions customConversions() {
final List<Converter> converters = new ArrayList<>();
converters.add(new ZonedDateTimeFromDateConverter());
converters.add(new ZonedDateTimeToDateConverter());
return new MongoCustomConversions(converters);
}
@Bean
公共MongoCustomConversions customConversions(){
最终列表转换器=新的ArrayList();
添加(新的ZonedDateTimeFromDateConverter());
添加(新的ZonedDateTimeToDateConverter());
返回新的MongoCustomConversions(转换器);
}
另一个提示是,mongo返回UTC()格式的日期,这意味着您将获得UTC格式的时间,而不是ZoneId.systemDefault()格式的时间。我的反序列化程序如下所示:
private static final ZoneId DEFAULT_ZONE_READ = ZoneId.of("UTC");
public static class ZonedDateTimeFromDateConverter implements Converter<Date, ZonedDateTime> {
@Override
public ZonedDateTime convert(Date date) {
return date.toInstant().atZone(DEFAULT_ZONE_READ);
}
}
private static final ZoneId DEFAULT_ZONE_READ=ZoneId.of(“UTC”);
公共静态类ZonedDateTimeFromDateConverter实现转换器{
@凌驾
公共分区日期时间转换(日期){
返回日期.toInstant().atZone(默认区域读取);
}
}
您可以添加其余的代码和配置吗?您使用的是MongoRepository
还是MongoTemplate
?当我尝试使用您的代码时,我一直无法获得名为“mongoTemplate”的bean。公共接口UserRepository扩展了MongoRepository{..}似乎CustomConversions没有得到调用。我无法运行您的安装程序。但是如果我必须猜测,请尝试使用@Import(value={MongoAutoConfiguration.class,MongoDataAutoConfiguration.class})
创建一个新的导入?
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
public final class JSRConverters {
private JSRConverters() {}
public static class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
public static final ZonedDateTimeToDateConverter INSTANCE = new ZonedDateTimeToDateConverter();
private ZonedDateTimeToDateConverter() {}
@Override
public Date convert(final ZonedDateTime source) {
return source == null ? null : Date.from(source.toInstant());
}
}
public static class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
public static final DateToZonedDateTimeConverter INSTANCE = new DateToZonedDateTimeConverter();
private DateToZonedDateTimeConverter() {}
@Override
public ZonedDateTime convert(final Date source) {
return source == null ? null : ZonedDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
}
}
}
@Bean
public MongoCustomConversions customConversions() {
final List<Converter> converters = new ArrayList<>();
converters.add(new ZonedDateTimeFromDateConverter());
converters.add(new ZonedDateTimeToDateConverter());
return new MongoCustomConversions(converters);
}
private static final ZoneId DEFAULT_ZONE_READ = ZoneId.of("UTC");
public static class ZonedDateTimeFromDateConverter implements Converter<Date, ZonedDateTime> {
@Override
public ZonedDateTime convert(Date date) {
return date.toInstant().atZone(DEFAULT_ZONE_READ);
}
}