spring-boot中配置Mongodbd的问题小结
错误1:
Description: Parameter 1 of method standardMongoSettingsCustomizer in org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$MongoClientSettingsConfiguration required a bean of type
'org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails' in your configuration.
注意版本,由于我开始写了版本号,如下:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> <version>4.0.0</version> 最好去掉版本号,自动寻找兼容的 </dependency>
改成如下:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
错误2:
Caused by: com.mongodb.MongoQueryException: Command failed with error 2 (BadValue): 'Field 'locale' is invalid in: { locale: "userinfo" }' on server localhost:27017.
The full response is {"ok": 0.0, "errmsg": "Field 'locale' is invalid in: { locale: \"userinfo\" }", "code": 2, "codeName": "BadValue"}
这个问题我找了很久的原因,
问题原因:
- 在
@Document注解中,你很可能将collection写成了collation collation用于指定字符串比较规则,而collection才是用来指定集合名称的- 由于这两个属性名称非常相似,很容易混淆写错
解决方法:
将注解中的 collation 改为 collection:
// 错误写法 @Document(collation = "userinfo") // 正确写法 @Document(collection = "userinfo")
浙公网安备 33010602011771号