通过SpringData将LocaTime保存到MongoDB

时间:2015-05-12 06:30:23

标签: mongodb spring-data jodatime

我尝试使用spring-boot-starter-parent(org.springframework.boot 1.2.3.RELEASE)使用SpringData将LocalTime(joda)字段保存到MongoDB并获取StackOverflowError。

StackOverflowError在方法

中的BeanWrapper中
public <S> S getProperty(PersistentProperty<?> property, Class<? extends S> type)

堆栈跟踪:

http-nio-8080-exec-2@5509 daemon, prio=5, in group 'main', status: 'RUNNING'
  at org.springframework.data.mapping.model.BeanWrapper.getProperty(BeanWrapper.java:120)
  at org.springframework.data.mapping.model.BeanWrapper.getProperty(BeanWrapper.java:100)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:419)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:412)
  at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:307)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:412)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:511)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:424)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:412)
  at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:307)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:412)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:511)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:424)
  at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:412)
  at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:307)...

1 个答案:

答案 0 :(得分:0)

将这两个转换器添加到CustomConversions可以解决问题。

@Configuration
public class MongoConfiguration extends AbstractMongoConfiguration {
    @Override
    protected String getDatabaseName() {
        return "databasename";
    }

    @Override
    public Mongo mongo() throws Exception {
        return new MongoClient("localhost");
    }

    @Override
    public CustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(new LocalTimeToStringConverter());
        converters.add(new StringToLocalTimeConverter());
        return new CustomConversions(converters);
    }

}

public class LocalTimeToStringConverter implements Converter<LocalTime, String> {
    @Override
    public String convert(LocalTime localTime) {
        return localTime.toString();
    }
}



public class StringToLocalTimeConverter implements Converter<String, LocalTime> {
    @Override
    public LocalTime convert(String s) {
        return LocalTime.parse(s);
    }
}