在实体类 java.time.ZonedDateTime 上找不到将构造函数参数绑定到

我有以下配置

配置

@Configuration
@EnableMongoRepositories(basePackages = Constants.DATA_SCAN)
@EnableMongoAuditing(auditorAwareRef = "auditorAwareService")
@Import(value = MongoAutoConfiguration.class)
public class DatabaseConfiguration {

    @Bean
    public ValidatingMongoEventListener validatingMongoEventListener() {
        return new ValidatingMongoEventListener(validator());
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }

    @Bean
    public CustomConversions customConversions() {
        final List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(DateToZonedDateTimeConverter.INSTANCE);
        converters.add(ZonedDateTimeToDateConverter.INSTANCE);
        return new CustomConversions(converters);
    }
}

我添加了自定义转换器,但我仍然得到:

在实体类 java.time.ZonedDateTime 上找不到将构造函数参数绑定到的属性 null!

@Document(collection = "user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String id;

    @Field("reset_date")
    private ZonedDateTime resetDate = null;
}

啪.xml

<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());
        }
    }
}

答案 1

我有相同的设置工作,但使用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);
}

另一个提示,mongo以UTC(https://docs.mongodb.com/manual/reference/method/Date/)返回日期,这意味着您将获得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);
    }
}

答案 2

推荐