前阵子在使用 mongo 查询时碰到了一个很神奇的报错信息,而且该报错信息仅在服务器启动后第一次查询时会产生,后续继续使用就又正常了。针对该问题排查许久未果。最近又重新断点排查了一遍,终于发现了具体报错的原因。
其实报错原因很简单,就是在使用注解定义索引时使用了中文的冒号(:),底层代码无法解析该冒号,所以就报错了。
我们项目的服务器启动后的首次查询时,会遍历代码中注解的索引,如果该索引在 mongo 中不存在,则为 mongo 创建索引,已存在则不再处理。因此造成的现象就是每次服务器启动后,第一次查询时,总会报出题目中的错误。
如果有碰到类似问题的小伙伴,可以往这个方向排查看看。
大概的堆栈如下:
org.bson.json.JsonParseException: Invalid JSON input. Position: 16. Character: ':'.
at org.bson.json.JsonScanner.nextToken(JsonScanner.java:99)
at org.bson.json.JsonReader.popToken(JsonReader.java:492)
at org.bson.json.JsonReader.readBsonType(JsonReader.java:130)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:149)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45)
at org.bson.Document.parse(Document.java:105)
at org.bson.Document.parse(Document.java:90)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveCompoundIndexKeyFromStringDefinition(MongoPersistentEntityIndexResolver.java:376)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.createCompoundIndexDefinition(MongoPersistentEntityIndexResolver.java:345)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.lambda$createCompoundIndexDefinitions$2(MongoPersistentEntityIndexResolver.java:327)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.createCompoundIndexDefinitions(MongoPersistentEntityIndexResolver.java:328)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.potentiallyCreateCompoundIndexDefinitions(MongoPersistentEntityIndexResolver.java:211)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexForEntity(MongoPersistentEntityIndexResolver.java:109)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexFor(MongoPersistentEntityIndexResolver.java:90)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:134)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:127)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:111)
at com.exe.mongo.client.factory.MongoMappingEventPublisher.publish(MongoMappingEventPublisher.java:30)
at com.exe.mongo.client.factory.MongoMappingEventPublisher.publishEvent(MongoMappingEventPublisher.java:18)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:420)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:229)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:172)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:85)
at org.springframework.data.mapping.context.MappingContext.getRequiredPersistentEntity(MappingContext.java:70)