org.springframework.data.mapping.PreferredConstructor$Parameter@2514e415 does not have a name! (Spring data mongo converters)
Asked Answered
C

0

8

I have this json in my Mongo collection(as field of main object):

"dateOfBirth" : {
        "year" : 1953,
        "month" : 4,
        "day" : 26
    }

And I have this Java class for mapping:

@Data
public class LocalDate {

    private Integer year;
    private Integer month;
    private Integer day;

    public LocalDate(Integer year, Integer month, Integer day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public LocalDate() {
    }
}

If I use this field, all work fine. spring data maps JSON to my LocalDate

    @Field("dateOfBirth")
    private com.my.LocalDate dateOfBirth;

But I want to use this field as java.time.LocalDate and I created converters:

@ReadingConverter
public class MyLocalDateToJavaLocalDateConverter implements Converter<LocalDate, java.time.LocalDate> {
    @Override
    public java.time.LocalDate convert(LocalDate localDate) {
        return java.time.LocalDate.of(localDate.getYear(), localDate.getMonth(), localDate.getDay());
    }
}

@WritingConverter
public class JavaLocalDateToMyLocalDateConverter implements Converter<java.time.LocalDate, LocalDate> {
    @Override
    public LocalDate convert(java.time.LocalDate localDate) {
        return new LocalDate(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfWeek().getValue());
    }
}

And configuration:

@Configuration
public class MongoConvertersConfig {

    @Bean
    public MongoCustomConversions mongoCustomConversions() {
        return new MongoCustomConversions(
                Arrays.asList(
                        new MyLocalDateToJavaLocalDateConverter(),
                        new JavaLocalDateToMyLocalDateConverter()));
    }
}

But when I select data I get an exception:

org.springframework.data.mapping.MappingException: Parameter org.springframework.data.mapping.PreferredConstructor$Parameter@2514e415 does not have a name!
Catharinecatharsis answered 30/10, 2020 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.