MongoDB,Spring Data,findAll()方法错误:
找不到能够从类型[java.lang.String]转换的转换器 输入[java.time.LocalDateTime]
public String query_partial_match(String skuid, List<String> attrKey) {
return "MATCH (s:Product {id:'" + skuid + "'})-[r]-> (o) " +
"WHERE " +
"(o:ExAttrs OR o:ProdAttrs) AND " +
"any(key in keys(o) WHERE key =~'(?i).*(" + attrKey + ").*') " +
"return o";
}
我可以保存实体,但无法加载它。任何快速修复?
答案 0 :(得分:2)
这解决了这个问题:
@Configuration
public class MongoConfiguration extends AbstractMongoConfiguration {
@Value("${spring.data.mongodb.database:test}")
private String database;
@Value("${spring.data.mongodb.host:localhost}:${spring.data.mongodb.port:27017}")
private String host;
@Autowired
private MappingMongoConverter mongoConverter;
// Converts . into a mongo friendly char
@PostConstruct
public void setUpMongoEscapeCharacterConversion() {
mongoConverter.setMapKeyDotReplacement("_");
}
@Override
protected String getDatabaseName() {
return database;
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient(host);
}
@Bean
@Override
public CustomConversions customConversions() {
List<Converter<?, ?>> converterList = new ArrayList<Converter<?, ?>>();
converterList.add(new MongoLocalDateTimeFromStringConverter());
return new CustomConversions(converterList);
}
private static final class MongoLocalDateTimeFromStringConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
return source == null ? null : LocalDateTime.parse(source);
}
}
}
答案 1 :(得分:0)
@Maksym的方式也对我有所帮助。对于Spring Framework 5和Spring Boot 2,我不得不对其进行一些调整。
@Bean
@Primary
public MappingMongoConverter mongoConverter(
@Autowired MongoMappingContext mongoMappingContext,
@Autowired MongoDbFactory mainMongoFactory,
@Autowired MongoCustomConversions conversions
) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mainMongoFactory);
MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
mongoConverter.setMapKeyDotReplacement("#");
mongoConverter.afterPropertiesSet();
mongoConverter.setCustomConversions(conversions);
return mongoConverter;
}
@Bean
public MongoMappingContext mongoMappingContext() {
MongoMappingContext context = new MongoMappingContext();
context.setSimpleTypeHolder(new SimpleTypeHolder(new HashSet<>(Arrays.asList(
DateTime.class,
LocalDateTime.class
)), MongoSimpleTypes.HOLDER));
return context;
}
@Bean
public MongoCustomConversions customConversions() {
List<Converter<?, ?>> converterList = new ArrayList<Converter<?, ?>>();
converterList.add(new MongoLocalDateTimeFromStringConverter());
converterList.add(new MongoDateTimeFromStringConverter());
return new MongoCustomConversions(converterList);
}
private static final class MongoLocalDateTimeFromStringConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
return source == null ? null : LocalDateTime.parse(source);
}
}
private static final class MongoDateTimeFromStringConverter implements Converter<String, DateTime> {
@Override
public DateTime convert(String source) {
return source == null ? null : DateTime.parse(source);
}
}
答案 2 :(得分:0)
Ref-https://github.com/lordofthejars/nosql-unit#dataset-format
If you want to use ISODate function or any other javascript function you should see how MongoDB Java Driver deals with it. For example in case of ISODate:
在您的json文件中,使用$ date进行转换
"bornAt":{ "$date" : "2011-01-05T10:09:15.210Z"}