No property null found on entity class java.time.ZonedDateTime to bind constructor parameter to
Asked Answered
A

1

19

I have the following configuration

Configuration

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

I added custom converters but I am still getting:

No property null found on entity class java.time.ZonedDateTime to bind constructor parameter to!

@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;
}

pom.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>

Converter class

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());
        }
    }
}
Agrippina answered 9/2, 2017 at 9:31 Comment(10)
Can you add rest of the code and configuration ? Do you use MongoRepository or MongoTemplate ? I keep getting No bean named 'mongoTemplate' available when I try to use your code.Hamburger
public interface UserRepository extends MongoRepository<User, String> {..}Agrippina
Seems like the CustomConversions are not getting invokedAgrippina
I cant run your setup. But if I have to guess try using @Import(value = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})Hamburger
No its same exception even after adding MongoDataAutoConfiguration.Agrippina
so i was trying out the following github.com/jhipster/jhipster-sample-app-mongodb but seems like it dont workAgrippina
I have answered something similar here https://mcmap.net/q/639057/-zoneddatetime-with-mongodb/…. See if it helps.Hamburger
yeah i saw that and tried but no luck.Agrippina
Can you create a Minimal, Complete, and Verifiable ?Hamburger
Try removing the default null assignment to resetDate. it might be causing trouble with the annotationAmperehour
A
1

i have the same setup working, but using org.springframework.data.mongodb.core.convert.MongoCustomConversions instead of org.springframework.data.mongodb.core.convert.CustomConversions

@Bean
public MongoCustomConversions customConversions() {

    final List<Converter> converters = new ArrayList<>();
    converters.add(new ZonedDateTimeFromDateConverter());
    converters.add(new ZonedDateTimeToDateConverter());

    return new MongoCustomConversions(converters);
}

and another hint, mongo returns the date in UTC (https://docs.mongodb.com/manual/reference/method/Date/), this means you'll get the time in UTC and not in you'r ZoneId.systemDefault(). My deserializer looks like this:

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);
    }
}
Asleep answered 2/6, 2018 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.