我今天刚升级到Spring Boot 2.0.2,也是Spring-Data-MongoDB 2.0.7的一部分。
我有以下实体:
@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;
}
}
在我的测试中,我使用以下内容插入新用户:
mongoTemplate.save(new User("testuser", "$2a$04$OuKj.lXzPxlwgr2Jy28E4ehHuxnVZ7BuL46qX9fd6vAijcDN6UeHe", Collections.singletonList("user")));
使用Spring Boot 2.0.1(分别是Spring-Data-MongoDB 2.0.6)时,这非常有效。使用升级的Spring-Data-MongoDB版本,我收到以下错误:
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
所以看起来,它想要将username
转换为ZonedDateTime
。问题可能是我的MongoConfig,我有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);
}
}
}
当然,这些转换器不应该用于普通字符串,但它似乎是在使用新的Spring Data MongoDB版本时触发的。任何人都可以请指出我的代码问题或最新版本有问题吗?
提前致谢!
答案 0 :(得分:0)
更新到版本2后,我遇到了同样的问题。
我的问题是我们在转换器中使用DBObject
而不是org.bson.Document
。例如:
public class MyCustomMappingConverter implements Converter<DBObject, MyClass>
我将其更改为:
public class MyCustomMappingConverter implements Converter<Document, MyClass>