Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Data-MongoDB: Failed to convert from type after upgrade to 2.0.7 with custom converter

I just upgraded to Spring Boot 2.0.2 today and as part of that also to Spring-Data-MongoDB 2.0.7.

I have the following entity:

@Document
@Data
public class User {
private String username;
private String password;
private List<String> authorities;

    public User(String username, String password, List<String> authorities) {
        this.username = username;
        this.password = password;
        this.authorities = authorities;
    }
}

In a test of mine I insert a new User using the following:

mongoTemplate.save(new User("testuser", "$2a$04$OuKj.lXzPxlwgr2Jy28E4ehHuxnVZ7BuL46qX9fd6vAijcDN6UeHe", Collections.singletonList("user")));

This worked perfectly when using Spring Boot 2.0.1 (respectively Spring-Data-MongoDB 2.0.6). With the upgraded Spring-Data-MongoDB version, I get the following error:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.ZonedDateTime] for value 'testuser'; nested exception is java.time.format.DateTimeParseException: Text 'testuser' could not be parsed at index 0

    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:46)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleWrite(MappingMongoConverter.java:849)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeSimpleInternal(MappingMongoConverter.java:829)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:488)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:462)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:436)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:391)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:86)
    at org.springframework.data.mongodb.core.MongoTemplate.toDocument(MongoTemplate.java:1070)
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1253)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:1201)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:1185)
....
Caused by: java.time.format.DateTimeParseException: Text 'testuser' could not be parsed at index 0
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
    at at.riag.scanpay.config.MongoConfig$StringToZonedDateTime.convert(MongoConfig.java:34)
    at at.riag.scanpay.config.MongoConfig$StringToZonedDateTime.convert(MongoConfig.java:28)
    at org.springframework.core.convert.support.GenericConversionService$ConverterAdapter.convert(GenericConversionService.java:385)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:40)
    ... 71 more

So it seems like, it wants to convert the username to a ZonedDateTime. The problem could be my MongoConfig, where I have custom converters for ZonedDateTime:

@Configuration
public class MongoConfig {

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;

    @Bean
    public MongoCustomConversions customConversions(){
        List<Converter<?,?>> converters = new ArrayList<>();
        converters.add(ZonedDateTimeToStringConverter.INSTANCE);
        converters.add(StringToZonedDateTime.INSTANCE);
        return new MongoCustomConversions(converters);
    }

    enum StringToZonedDateTime implements Converter<String, ZonedDateTime> {

        INSTANCE;

        @Override
        public ZonedDateTime convert(String source) {
            return ZonedDateTime.parse(source, dateTimeFormatter);
        }
    }

    enum ZonedDateTimeToStringConverter implements Converter<ZonedDateTime, String> {

        INSTANCE;

        @Override
        public String convert(ZonedDateTime source) {
            return dateTimeFormatter.format(source);
        }
    }
}

These converters should not be used for a plain String of course but it seems that it is triggered when using the new Spring Data MongoDB version. Can anyone please point me to the problem I am having with my code or is there a problem with the latest version?

Thanks in advance!

like image 333
Michael Altenburger Avatar asked May 11 '18 07:05

Michael Altenburger


2 Answers

I had the same problem after updating it to version 2.

My problem was that we were using DBObject in the converter instead of org.bson.Document. For example:

public class MyCustomMappingConverter implements Converter<DBObject, MyClass>

I solved it by changing it to:

public class MyCustomMappingConverter implements Converter<Document, MyClass>
like image 54
Wilder Pereira Avatar answered Oct 14 '22 01:10

Wilder Pereira


Spring Data MongoDB 2.0.7 considers ZonedDateType as simple type. You need to disambiguate converters into reading and writing converters by adding @ReadingConverter/@WritingConverter.

In your arrangement above, the framework attempts to convert all String values into ZonedDateTime. Instead, you want to represent rather ZonedDateTime objects as String in your MongoDB. So adding @WritingConverter to ZonedDateTimeToStringConverter and @ReadingConverter to the other one fixes your issue.

like image 28
mp911de Avatar answered Oct 14 '22 00:10

mp911de