Spring MongoDB - org.bson.codecs.configuration.CodecConfigurationException:无法找到类java.lang.Class

时间:2016-05-04 08:05:30

标签: spring mongodb spring-boot spring-data-mongodb spring-mongo

我正在为即将推出的项目之一的Spring启动应用程序中评估MongoDB和Spring Data MongoDB。我有一个简单的POJO,可以保存在MongoDB中

@Document(collection = "customers")
public class CustomerDoc{
@org.springframework.data.annotation.Id
private String Id;
private String firstName;
private String lastName;
private MonetaryAmount amount;

    public CustomerDoc(String firstName, String lastName, MonetaryAmount    amount){
         this.firstName = firstName;
         this.lastName = lastName;
         this.amount = amount;
       }
}

Spring配置

@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    return new SimpleMongoDbFactory(mongoClient, mongoDbName);
}

@Bean
public MongoTemplate mongoTemplate() throws Exception {
    return new MongoTemplate(mongoDbFactory(), mongoConverter());
}

@Bean
public MappingMongoConverter mongoConverter() throws Exception {
    MongoMappingContext mappingContext = new MongoMappingContext();
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
    MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mappingContext);
    mongoConverter.setMapKeyDotReplacement("\\:");
    return mongoConverter;
}

在配置中,我必须设置替换密钥,因为没有它,我得到例外:

  

org.springframework.data.mapping.model.MappingException:Map键java.lang.Class包含点但未配置替换!确保地图键首先不包含点或配置适当的替换!

这是因为MonetaryAmount

中的Customer对象

客户存储库是:

public interface CustomerDocRepository extends MongoRepository<CustomerDoc, String>{

    public List<CustomerDoc> findByFirstName();
}

现在,如果我尝试保存客户对象,我会遇到以下异常:

  

线程中的异常&#34; main&#34; java.lang.IllegalStateException:无法执行CommandLineRunner       在org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:675)       在org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:321)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:957)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:946)       在com.poc.mongodb.MongoDbApp.main(MongoDbApp.java:43)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:497)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)   由以下原因引起:org.bson.codecs.configuration.CodecConfigurationException:无法找到类java.lang.Class的编解码器。       在org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)       在org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)       在org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)       在com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:210)       在com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:220)       在com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:196)       在com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:220)       在com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:196)       在com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:220)       在com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:196)       在com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:128)       在com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:61)       在com.mongodb.CompoundDBObjectCodec.encode(CompoundDBObjectCodec.java:48)       在com.mongodb.CompoundDBObjectCodec.encode(CompoundDBObjectCodec.java:27)       在org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)       在org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)       在com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:101)       在com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:43)       at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:129)       在com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160)       在com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:212)       在com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:101)       在com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:67)       在com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:37)       在com.mongodb.connection.DefaultServer $ DefaultServerProtocolExecutor.execute(DefaultServer.java:159)       at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)       在com.mongodb.connection.DefaultServerConnection.insertCommand(DefaultServerConnection.java:115)       在com.mongodb.operation.InsertOperation.executeCommandProtocol(InsertOperation.java:76)       在com.mongodb.operation.BaseWriteOperation $ 1.call(BaseWriteOperation.java:141)       在com.mongodb.operation.BaseWriteOperation $ 1.call(BaseWriteOperation.java:133)       在com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:230)       在com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)       在com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:133)       在com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:60)       在com.mongodb.Mongo.execute(Mongo.java:781)       在com.mongodb.Mongo $ 2.execute(Mongo.java:764)       在com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:333)       在com.mongodb.DBCollection.insert(DBCollection.java:328)       在com.mongodb.DBCollection.insert(DBCollection.java:319)       在com.mongodb.DBCollection.insert(DBCollection.java:289)       在com.mongodb.DBCollection.insert(DBCollection.java:255)       在com.mongodb.DBCollection.insert(DBCollection.java:239)       在org.springframework.data.mongodb.core.MongoTemplate $ 10.doInCollection(MongoTemplate.java:1054)       在org.springframework.data.mongodb.core.MongoTemplate $ 10.doInCollection(MongoTemplate.java:1049)       在org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:462)       在org.springframework.data.mongodb.core.MongoTemplate.insertDBObjectList(MongoTemplate.java:1049)       在org.springframework.data.mongodb.core.MongoTemplate.doInsertBatch(MongoTemplate.java:935)

我正在使用mongo驱动程序3.2.2 Spring数据MongoDB 1.9.1

任何帮助都会非常感激

0 个答案:

没有答案